mirror of
https://github.com/zed-industries/zed.git
synced 2026-12-02 13:30:26 +01:00
gpui: Compute tab stop order only when moving focus (#66721)
## Summary `TabStopMap` inserted every focusable element into a `SumTree` on every frame, but the order is only read when focus moves with tab. This records nodes in a `Vec` and sorts on demand. Stacked on #66719. ## Benchmarks Change vs `main`, `randomized_element_tree`, macOS (Apple Silicon), 2 alternating runs: | Workload | Instructions | Allocations | Bytes allocated | |---|---:|---:|---:| | Path-free full refresh, wide | −12.4% to −19.9% | −7.8% to −12.1% | −43% to −57% | | Path-free full refresh, dense / mixed | −2.1% to −11.0% | −1.9% to −7.9% | −13% to −45% | | Full refresh with paths | −0.3% to −4.2% | −0.1% to −1.9% | −0.5% to −4.4% | | Text, scrolling | ~0% | 0% | 0% | Release Notes: - N/A - [GPUI] Change tab stop internal data structure from a SumTree to a Vector for performance and memory savings
This commit is contained in:
+84
-199
@@ -1,18 +1,22 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use ::sum_tree::SumTree;
|
||||
use collections::FxHashMap;
|
||||
use sum_tree::Bias;
|
||||
|
||||
use crate::{FocusHandle, FocusId};
|
||||
|
||||
/// Represents a collection of focus handles using the tab-index APIs.
|
||||
#[derive(Debug)]
|
||||
///
|
||||
/// Every focusable element is inserted on every frame, while the tab order is only needed
|
||||
/// when the user moves focus with tab. So insertion just appends the node, and the nodes
|
||||
/// are sorted in place the first time the order is needed after an insertion.
|
||||
#[derive(Debug, Default)]
|
||||
pub(crate) struct TabStopMap {
|
||||
current_path: TabStopPath,
|
||||
pub(crate) insertion_history: Vec<TabStopOperation>,
|
||||
by_id: FxHashMap<FocusId, TabStopNode>,
|
||||
order: SumTree<TabStopNode>,
|
||||
/// Index into `nodes` of each focus handle's most recent insertion.
|
||||
by_id: FxHashMap<FocusId, usize>,
|
||||
nodes: Vec<TabStopNode>,
|
||||
sorted: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -63,30 +67,19 @@ impl PartialOrd for TabStopNode {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for TabStopMap {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
current_path: TabStopPath::default(),
|
||||
insertion_history: Vec::new(),
|
||||
by_id: FxHashMap::default(),
|
||||
order: SumTree::new(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TabStopMap {
|
||||
pub fn insert(&mut self, focus_handle: &FocusHandle) {
|
||||
self.insertion_history
|
||||
.push(TabStopOperation::Insert(focus_handle.clone()));
|
||||
let mut path = self.current_path.clone();
|
||||
path.0.push(focus_handle.tab_index);
|
||||
let order = TabStopNode {
|
||||
self.by_id.insert(focus_handle.id, self.nodes.len());
|
||||
self.sorted = false;
|
||||
self.nodes.push(TabStopNode {
|
||||
node_insertion_index: self.insertion_history.len() - 1,
|
||||
tab_stop: focus_handle.tab_stop,
|
||||
path,
|
||||
};
|
||||
self.by_id.insert(focus_handle.id, order.clone());
|
||||
self.order.insert_or_replace(order, ());
|
||||
});
|
||||
}
|
||||
|
||||
pub fn begin_group(&mut self, tab_index: isize) {
|
||||
@@ -101,85 +94,60 @@ impl TabStopMap {
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
*self = Self::default();
|
||||
self.current_path.0.clear();
|
||||
self.insertion_history.clear();
|
||||
self.by_id.clear();
|
||||
self.order = SumTree::new(());
|
||||
self.nodes.clear();
|
||||
self.sorted = false;
|
||||
}
|
||||
|
||||
pub fn next(&self, focused_id: Option<&FocusId>) -> Option<FocusHandle> {
|
||||
let Some(focused_id) = focused_id else {
|
||||
let first = self.order.first()?;
|
||||
if first.tab_stop {
|
||||
return self.focus_handle_for_order(first);
|
||||
} else {
|
||||
return self
|
||||
.next_inner(first)
|
||||
.and_then(|order| self.focus_handle_for_order(order));
|
||||
pub fn next(&mut self, focused_id: Option<&FocusId>) -> Option<FocusHandle> {
|
||||
self.ensure_sorted();
|
||||
let start = focused_id
|
||||
.and_then(|id| self.by_id.get(id))
|
||||
.map_or(0, |position| position + 1);
|
||||
let node = self.nodes[start..]
|
||||
.iter()
|
||||
.chain(&self.nodes[..start])
|
||||
.find(|node| node.tab_stop)?;
|
||||
self.focus_handle_for_order(node)
|
||||
}
|
||||
|
||||
pub fn prev(&mut self, focused_id: Option<&FocusId>) -> Option<FocusHandle> {
|
||||
self.ensure_sorted();
|
||||
let end = focused_id
|
||||
.and_then(|id| self.by_id.get(id).copied())
|
||||
.unwrap_or(self.nodes.len());
|
||||
let node = self.nodes[..end]
|
||||
.iter()
|
||||
.rev()
|
||||
.chain(self.nodes[end..].iter().rev())
|
||||
.find(|node| node.tab_stop)?;
|
||||
self.focus_handle_for_order(node)
|
||||
}
|
||||
|
||||
fn ensure_sorted(&mut self) {
|
||||
if self.sorted {
|
||||
return;
|
||||
}
|
||||
// Nodes never compare equal, since their insertion indices differ, so an unstable
|
||||
// sort gives the same order as a stable one without allocating a scratch buffer.
|
||||
self.nodes.sort_unstable();
|
||||
self.by_id.clear();
|
||||
for (position, node) in self.nodes.iter().enumerate() {
|
||||
let Some(focus_handle) = self
|
||||
.insertion_history
|
||||
.get(node.node_insertion_index)
|
||||
.and_then(TabStopOperation::focus_handle)
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let latest = self.by_id.entry(focus_handle.id).or_insert(position);
|
||||
if self.nodes[*latest].node_insertion_index < node.node_insertion_index {
|
||||
*latest = position;
|
||||
}
|
||||
};
|
||||
|
||||
let Some(node) = self.tab_node_for_focus_id(focused_id) else {
|
||||
return self.next(None);
|
||||
};
|
||||
let item = self.next_inner(node);
|
||||
|
||||
if let Some(item) = item {
|
||||
self.focus_handle_for_order(&item)
|
||||
} else {
|
||||
self.next(None)
|
||||
}
|
||||
}
|
||||
|
||||
fn next_inner(&self, node: &TabStopNode) -> Option<&TabStopNode> {
|
||||
let mut cursor = self.order.cursor::<TabStopNode>(());
|
||||
cursor.seek(&node, Bias::Left);
|
||||
cursor.next();
|
||||
while let Some(item) = cursor.item()
|
||||
&& !item.tab_stop
|
||||
{
|
||||
cursor.next();
|
||||
}
|
||||
|
||||
cursor.item()
|
||||
}
|
||||
|
||||
pub fn prev(&self, focused_id: Option<&FocusId>) -> Option<FocusHandle> {
|
||||
let Some(focused_id) = focused_id else {
|
||||
let last = self.order.last()?;
|
||||
if last.tab_stop {
|
||||
return self.focus_handle_for_order(last);
|
||||
} else {
|
||||
return self
|
||||
.prev_inner(last)
|
||||
.and_then(|order| self.focus_handle_for_order(order));
|
||||
}
|
||||
};
|
||||
|
||||
let Some(node) = self.tab_node_for_focus_id(focused_id) else {
|
||||
return self.prev(None);
|
||||
};
|
||||
let item = self.prev_inner(node);
|
||||
|
||||
if let Some(item) = item {
|
||||
self.focus_handle_for_order(&item)
|
||||
} else {
|
||||
self.prev(None)
|
||||
}
|
||||
}
|
||||
|
||||
fn prev_inner(&self, node: &TabStopNode) -> Option<&TabStopNode> {
|
||||
let mut cursor = self.order.cursor::<TabStopNode>(());
|
||||
cursor.seek(&node, Bias::Left);
|
||||
cursor.prev();
|
||||
while let Some(item) = cursor.item()
|
||||
&& !item.tab_stop
|
||||
{
|
||||
cursor.prev();
|
||||
}
|
||||
|
||||
cursor.item()
|
||||
self.sorted = true;
|
||||
}
|
||||
|
||||
pub fn replay(&mut self, nodes: &[TabStopOperation]) {
|
||||
@@ -197,7 +165,10 @@ impl TabStopMap {
|
||||
}
|
||||
|
||||
pub(crate) fn tab_stop_count(&self) -> usize {
|
||||
self.by_id.values().filter(|node| node.tab_stop).count()
|
||||
self.by_id
|
||||
.values()
|
||||
.filter(|index| self.nodes.get(**index).is_some_and(|node| node.tab_stop))
|
||||
.count()
|
||||
}
|
||||
|
||||
fn focus_handle_for_order(&self, order: &TabStopNode) -> Option<FocusHandle> {
|
||||
@@ -208,108 +179,6 @@ impl TabStopMap {
|
||||
);
|
||||
handle.cloned()
|
||||
}
|
||||
|
||||
fn tab_node_for_focus_id(&self, focused_id: &FocusId) -> Option<&TabStopNode> {
|
||||
let Some(order) = self.by_id.get(focused_id) else {
|
||||
return None;
|
||||
};
|
||||
Some(order)
|
||||
}
|
||||
}
|
||||
|
||||
mod sum_tree_impl {
|
||||
use sum_tree::SeekTarget;
|
||||
|
||||
use crate::tab_stop::{TabStopNode, TabStopPath};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct TabStopOrderNodeSummary {
|
||||
max_index: usize,
|
||||
max_path: TabStopPath,
|
||||
pub tab_stops: usize,
|
||||
}
|
||||
|
||||
pub type TabStopCount = usize;
|
||||
|
||||
impl sum_tree::ContextLessSummary for TabStopOrderNodeSummary {
|
||||
fn zero() -> Self {
|
||||
TabStopOrderNodeSummary {
|
||||
max_index: 0,
|
||||
max_path: TabStopPath::default(),
|
||||
tab_stops: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn add_summary(&mut self, summary: &Self) {
|
||||
self.max_index = summary.max_index;
|
||||
self.max_path = summary.max_path.clone();
|
||||
self.tab_stops += summary.tab_stops;
|
||||
}
|
||||
}
|
||||
|
||||
impl sum_tree::KeyedItem for TabStopNode {
|
||||
type Key = Self;
|
||||
|
||||
fn key(&self) -> Self::Key {
|
||||
self.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl sum_tree::Item for TabStopNode {
|
||||
type Summary = TabStopOrderNodeSummary;
|
||||
|
||||
fn summary(&self, _cx: <Self::Summary as sum_tree::Summary>::Context<'_>) -> Self::Summary {
|
||||
TabStopOrderNodeSummary {
|
||||
max_index: self.node_insertion_index,
|
||||
max_path: self.path.clone(),
|
||||
tab_stops: if self.tab_stop { 1 } else { 0 },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> sum_tree::Dimension<'a, TabStopOrderNodeSummary> for TabStopCount {
|
||||
fn zero(_: <TabStopOrderNodeSummary as sum_tree::Summary>::Context<'_>) -> Self {
|
||||
0
|
||||
}
|
||||
|
||||
fn add_summary(
|
||||
&mut self,
|
||||
summary: &'a TabStopOrderNodeSummary,
|
||||
_: <TabStopOrderNodeSummary as sum_tree::Summary>::Context<'_>,
|
||||
) {
|
||||
*self += summary.tab_stops;
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> sum_tree::Dimension<'a, TabStopOrderNodeSummary> for TabStopNode {
|
||||
fn zero(_: <TabStopOrderNodeSummary as sum_tree::Summary>::Context<'_>) -> Self {
|
||||
TabStopNode::default()
|
||||
}
|
||||
|
||||
fn add_summary(
|
||||
&mut self,
|
||||
summary: &'a TabStopOrderNodeSummary,
|
||||
_: <TabStopOrderNodeSummary as sum_tree::Summary>::Context<'_>,
|
||||
) {
|
||||
self.node_insertion_index = summary.max_index;
|
||||
self.path = summary.max_path.clone();
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b> SeekTarget<'a, TabStopOrderNodeSummary, TabStopNode> for &'b TabStopNode {
|
||||
fn cmp(
|
||||
&self,
|
||||
cursor_location: &TabStopNode,
|
||||
_: <TabStopOrderNodeSummary as sum_tree::Summary>::Context<'_>,
|
||||
) -> std::cmp::Ordering {
|
||||
Iterator::cmp(self.path.0.iter(), cursor_location.path.0.iter()).then(
|
||||
<usize as Ord>::cmp(
|
||||
&self.node_insertion_index,
|
||||
&cursor_location.node_insertion_index,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -477,20 +346,20 @@ mod tests {
|
||||
}
|
||||
|
||||
fn traverse_tab_map(
|
||||
&self,
|
||||
traverse: impl Fn(&TabStopMap, Option<&FocusId>) -> Option<FocusHandle>,
|
||||
&mut self,
|
||||
traverse: impl Fn(&mut TabStopMap, Option<&FocusId>) -> Option<FocusHandle>,
|
||||
) -> Vec<FocusId> {
|
||||
let mut last_focus_id = None;
|
||||
let mut found = vec![];
|
||||
for _ in 0..self.expected.len() {
|
||||
let handle = traverse(&self.tab_map, last_focus_id.as_ref()).unwrap();
|
||||
let handle = traverse(&mut self.tab_map, last_focus_id.as_ref()).unwrap();
|
||||
last_focus_id = Some(handle.id);
|
||||
found.push(handle.id);
|
||||
}
|
||||
found
|
||||
}
|
||||
|
||||
fn assert(self) {
|
||||
fn assert(mut self) {
|
||||
let mut expected = self.expected.iter().map(|(_, id)| *id).collect_vec();
|
||||
|
||||
// Check next order
|
||||
@@ -520,6 +389,22 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_insert_after_sorting() {
|
||||
let focus_map = Arc::new(RwLock::new(FocusMap::default()));
|
||||
let mut tab_index_map = TabStopMap::default();
|
||||
let second = FocusHandle::new(&focus_map).tab_stop(true).tab_index(1);
|
||||
let first = FocusHandle::new(&focus_map).tab_stop(true).tab_index(0);
|
||||
|
||||
tab_index_map.insert(&second);
|
||||
assert_eq!(tab_index_map.next(None), Some(second.clone()));
|
||||
|
||||
tab_index_map.insert(&first);
|
||||
assert_eq!(tab_index_map.next(None), Some(first.clone()));
|
||||
assert_eq!(tab_index_map.next(Some(&first.id)), Some(second.clone()));
|
||||
assert_eq!(tab_index_map.prev(Some(&first.id)), Some(second));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_with_disabled_tab_stop() {
|
||||
TabStopMapTest::new()
|
||||
|
||||
Reference in New Issue
Block a user