Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

gpui: Remove unused hash-based text layout cache (#66152)

Lukas Wirth2a74c8dff690
diff --git a/crates/gpui/src/text_system.rs b/crates/gpui/src/text_system.rs
index 2955db0073c..8a1738a3c8a 100644
--- a/crates/gpui/src/text_system.rs
+++ b/crates/gpui/src/text_system.rs
@@ -711,74 +711,6 @@ impl WindowTextSystem {
}
}
- /// Shape the given line using a caller-provided content hash as the cache key.
- ///
- /// This enables cache hits without materializing a contiguous `SharedString` for the text.
- /// If the cache misses, `materialize_text` is invoked to produce the `SharedString` for shaping.
- ///
- /// Contract (caller enforced):
- /// - Same `text_hash` implies identical text content (collision risk accepted by caller).
- /// - `text_len` should be the UTF-8 byte length of the text (helps reduce accidental collisions).
- ///
- /// Like [`Self::shape_line`], this must be used only for single-line text (no `\n`).
- pub fn shape_line_by_hash(
- &self,
- text_hash: u64,
- text_len: usize,
- font_size: Pixels,
- runs: &[TextRun],
- force_width: Option<Pixels>,
- materialize_text: impl FnOnce() -> SharedString,
- ) -> ShapedLine {
- let mut decoration_runs = SmallVec::<[DecorationRun; 32]>::new();
- for run in runs {
- if let Some(last_run) = decoration_runs.last_mut()
- && last_run.color == run.color
- && last_run.underline == run.underline
- && last_run.strikethrough == run.strikethrough
- && last_run.background_color == run.background_color
- {
- last_run.len += run.len as u32;
- continue;
- }
- decoration_runs.push(DecorationRun {
- len: run.len as u32,
- color: run.color,
- background_color: run.background_color,
- underline: run.underline,
- strikethrough: run.strikethrough,
- });
- }
-
- let mut used_force_width = force_width;
- let layout = self.layout_line_by_hash(
- text_hash,
- text_len,
- font_size,
- runs,
- used_force_width,
- || {
- let text = materialize_text();
- debug_assert!(
- text.find('\n').is_none(),
- "text argument should not contain newlines"
- );
- text
- },
- );
-
- // We only materialize actual text on cache miss; on hit we avoid allocations.
- // Since `ShapedLine` carries a `SharedString`, use an empty placeholder for hits.
- // NOTE: Callers must not rely on `ShapedLine.text` for content when using this API.
- let text: SharedString = SharedString::new_static("");
-
- ShapedLine {
- layout,
- text,
- decoration_runs,
- }
- }
-
/// Shape a multi line string of text, at the given font_size, for painting to the screen.
/// Subsets of the text can be styled independently with the `runs` parameter.
/// If `wrap_width` is provided, the line breaks will be adjusted to fit within the given width.
@@ -990,130 +922,6 @@ impl WindowTextSystem {
pub fn em_layout_width(&self, font_id: FontId, font_size: Pixels) -> Pixels {
self.layout_width(font_id, font_size, 'm')
}
-
- /// Probe the line layout cache using a caller-provided content hash, without allocating.
- ///
- /// Returns `Some(layout)` if the layout is already cached in either the current frame
- /// or the previous frame. Returns `None` if it is not cached.
- ///
- /// Contract (caller enforced):
- /// - Same `text_hash` implies identical text content (collision risk accepted by caller).
- /// - `text_len` should be the UTF-8 byte length of the text (helps reduce accidental collisions).
- pub fn try_layout_line_by_hash(
- &self,
- text_hash: u64,
- text_len: usize,
- font_size: Pixels,
- runs: &[TextRun],
- force_width: Option<Pixels>,
- ) -> Option<Arc<LineLayout>> {
- let mut last_run = None::<&TextRun>;
- let mut font_runs = mem::take(&mut *self.font_runs_buffer.borrow_mut());
- font_runs.clear();
-
- for run in runs.iter() {
- let decoration_changed = if let Some(last_run) = last_run
- && last_run.color == run.color
- && last_run.underline == run.underline
- && last_run.strikethrough == run.strikethrough
- // we do not consider differing background color relevant, as it does not affect glyphs
- // && last_run.background_color == run.background_color
- {
- false
- } else {
- last_run = Some(run);
- true
- };
-
- let font_id = self.resolve_font(&run.font);
- if let Some(font_run) = font_runs.last_mut()
- && font_id == font_run.font_id
- && !decoration_changed
- {
- font_run.len += run.len;
- } else {
- font_runs.push(FontRun {
- len: run.len,
- font_id,
- });
- }
- }
-
- let layout = self.line_layout_cache.try_layout_line_by_hash(
- text_hash,
- text_len,
- font_size,
- &font_runs,
- force_width,
- );
-
- *self.font_runs_buffer.borrow_mut() = font_runs;
-
- layout
- }
-
- /// Layout the given line of text using a caller-provided content hash as the cache key.
- ///
- /// This enables cache hits without materializing a contiguous `SharedString` for the text.
- /// If the cache misses, `materialize_text` is invoked to produce the `SharedString` for shaping.
- ///
- /// Contract (caller enforced):
- /// - Same `text_hash` implies identical text content (collision risk accepted by caller).
- /// - `text_len` should be the UTF-8 byte length of the text (helps reduce accidental collisions).
- pub fn layout_line_by_hash(
- &self,
- text_hash: u64,
- text_len: usize,
- font_size: Pixels,
- runs: &[TextRun],
- force_width: Option<Pixels>,
- materialize_text: impl FnOnce() -> SharedString,
- ) -> Arc<LineLayout> {
- let mut last_run = None::<&TextRun>;
- let mut font_runs = mem::take(&mut *self.font_runs_buffer.borrow_mut());
- font_runs.clear();
-
- for run in runs.iter() {
- let decoration_changed = if let Some(last_run) = last_run
- && last_run.color == run.color
- && last_run.underline == run.underline
- && last_run.strikethrough == run.strikethrough
- // we do not consider differing background color relevant, as it does not affect glyphs
- // && last_run.background_color == run.background_color
- {
- false
- } else {
- last_run = Some(run);
- true
- };
-
- let font_id = self.resolve_font(&run.font);
- if let Some(font_run) = font_runs.last_mut()
- && font_id == font_run.font_id
- && !decoration_changed
- {
- font_run.len += run.len;
- } else {
- font_runs.push(FontRun {
- len: run.len,
- font_id,
- });
- }
- }
-
- let layout = self.line_layout_cache.layout_line_by_hash(
- text_hash,
- text_len,
- font_size,
- &font_runs,
- force_width,
- materialize_text,
- );
-
- *self.font_runs_buffer.borrow_mut() = font_runs;
-
- layout
- }
}
#[derive(Hash, Eq, PartialEq)]
diff --git a/crates/gpui/src/text_system/line_layout.rs b/crates/gpui/src/text_system/line_layout.rs
index 10336f7a34d..26a202f0144 100644
--- a/crates/gpui/src/text_system/line_layout.rs
+++ b/crates/gpui/src/text_system/line_layout.rs
@@ -476,17 +476,6 @@ struct FrameCache {
wrapped_lines: FxHashMap<Arc<CacheKey>, Arc<WrappedLineLayout>>,
used_lines: Vec<Arc<CacheKey>>,
used_wrapped_lines: Vec<Arc<CacheKey>>,
-
- // Content-addressable caches keyed by caller-provided text hash + layout params.
- // These allow cache hits without materializing a contiguous `SharedString`.
- //
- // IMPORTANT: To support allocation-free lookups, we store these maps using a key type
- // (`HashedCacheKeyRef`) that can be computed without building a contiguous `&str`/`SharedString`.
- // On miss, we allocate once and store under an owned `HashedCacheKey`.
- lines_by_hash: FxHashMap<Arc<HashedCacheKey>, Arc<LineLayout>>,
- wrapped_lines_by_hash: FxHashMap<Arc<HashedCacheKey>, Arc<WrappedLineLayout>>,
- used_lines_by_hash: Vec<Arc<HashedCacheKey>>,
- used_wrapped_lines_by_hash: Vec<Arc<HashedCacheKey>>,
}
#[derive(Clone, Default)]
@@ -494,8 +483,6 @@ pub(crate) struct LineLayoutIndex {
font_generation: usize,
lines_index: usize,
wrapped_lines_index: usize,
- lines_by_hash_index: usize,
- wrapped_lines_by_hash_index: usize,
}
impl LineLayoutCache {
@@ -520,8 +507,6 @@ impl LineLayoutCache {
font_generation,
lines_index: frame.used_lines.len(),
wrapped_lines_index: frame.used_wrapped_lines.len(),
- lines_by_hash_index: frame.used_lines_by_hash.len(),
- wrapped_lines_by_hash_index: frame.used_wrapped_lines_by_hash.len(),
}
}
@@ -553,24 +538,6 @@ impl LineLayoutCache {
}
current_frame.used_wrapped_lines.push(key.clone());
}
-
- for key in &previous_frame.used_lines_by_hash
- [range.start.lines_by_hash_index..range.end.lines_by_hash_index]
- {
- if let Some((key, line)) = previous_frame.lines_by_hash.remove_entry(key) {
- current_frame.lines_by_hash.insert(key, line);
- }
- current_frame.used_lines_by_hash.push(key.clone());
- }
-
- for key in &previous_frame.used_wrapped_lines_by_hash
- [range.start.wrapped_lines_by_hash_index..range.end.wrapped_lines_by_hash_index]
- {
- if let Some((key, line)) = previous_frame.wrapped_lines_by_hash.remove_entry(key) {
- current_frame.wrapped_lines_by_hash.insert(key, line);
- }
- current_frame.used_wrapped_lines_by_hash.push(key.clone());
- }
}
pub fn truncate_layouts(&self, index: LineLayoutIndex) {
@@ -584,12 +551,6 @@ impl LineLayoutCache {
current_frame
.used_wrapped_lines
.truncate(index.wrapped_lines_index);
- current_frame
- .used_lines_by_hash
- .truncate(index.lines_by_hash_index);
- current_frame
- .used_wrapped_lines_by_hash
- .truncate(index.wrapped_lines_by_hash_index);
}
pub fn finish_frame(&self) {
@@ -603,11 +564,6 @@ impl LineLayoutCache {
current_frame.wrapped_lines.clear();
current_frame.used_lines.clear();
current_frame.used_wrapped_lines.clear();
-
- current_frame.lines_by_hash.clear();
- current_frame.wrapped_lines_by_hash.clear();
- current_frame.used_lines_by_hash.clear();
- current_frame.used_wrapped_lines_by_hash.clear();
}
pub fn layout_wrapped_line<Text>(
@@ -738,164 +694,6 @@ impl LineLayoutCache {
}
}
- /// Try to retrieve a previously-shaped line layout using a caller-provided content hash.
- ///
- /// This is a *non-allocating* cache probe: it does not materialize any text. If the layout
- /// is not already cached in either the current frame or previous frame, returns `None`.
- ///
- /// Contract (caller enforced):
- /// - Same `text_hash` implies identical text content (collision risk accepted by caller).
- /// - `text_len` should be the UTF-8 byte length of the text (helps reduce accidental collisions).
- pub fn try_layout_line_by_hash(
- &self,
- text_hash: u64,
- text_len: usize,
- font_size: Pixels,
- runs: &[FontRun],
- force_width: Option<Pixels>,
- ) -> Option<Arc<LineLayout>> {
- let _font_generation = self.clear_if_font_generation_changed();
- let key_ref = HashedCacheKeyRef {
- text_hash,
- text_len,
- font_size,
- runs,
- wrap_width: None,
- force_width,
- };
-
- let state = self.state.borrow();
- if let Some((_, layout)) = state.current_frame.lines_by_hash.iter().find(|(key, _)| {
- HashedCacheKeyRef {
- text_hash: key.text_hash,
- text_len: key.text_len,
- font_size: key.font_size,
- runs: key.runs.as_slice(),
- wrap_width: key.wrap_width,
- force_width: key.force_width,
- } == key_ref
- }) {
- return Some(layout.clone());
- }
-
- if let Some((_, layout)) = state.previous_frame.lines_by_hash.iter().find(|(key, _)| {
- HashedCacheKeyRef {
- text_hash: key.text_hash,
- text_len: key.text_len,
- font_size: key.font_size,
- runs: key.runs.as_slice(),
- wrap_width: key.wrap_width,
- force_width: key.force_width,
- } == key_ref
- }) {
- return Some(layout.clone());
- }
-
- None
- }
-
- /// Layout a line of text using a caller-provided content hash as the cache key.
- ///
- /// This enables cache hits without materializing a contiguous `SharedString` for `text`.
- /// If the cache misses, `materialize_text` is invoked to produce the `SharedString` for shaping.
- ///
- /// Contract (caller enforced):
- /// - Same `text_hash` implies identical text content (collision risk accepted by caller).
- /// - `text_len` should be the UTF-8 byte length of the text (helps reduce accidental collisions).
- pub fn layout_line_by_hash(
- &self,
- text_hash: u64,
- text_len: usize,
- font_size: Pixels,
- runs: &[FontRun],
- force_width: Option<Pixels>,
- materialize_text: impl FnOnce() -> SharedString,
- ) -> Arc<LineLayout> {
- let _font_generation = self.clear_if_font_generation_changed();
- let key_ref = HashedCacheKeyRef {
- text_hash,
- text_len,
- font_size,
- runs,
- wrap_width: None,
- force_width,
- };
-
- // Fast path: already cached (no allocation).
- let mut state = self.state.borrow_mut();
- if let Some((_, layout)) = state.current_frame.lines_by_hash.iter().find(|(key, _)| {
- HashedCacheKeyRef {
- text_hash: key.text_hash,
- text_len: key.text_len,
- font_size: key.font_size,
- runs: key.runs.as_slice(),
- wrap_width: key.wrap_width,
- force_width: key.force_width,
- } == key_ref
- }) {
- return layout.clone();
- }
-
- // Try to reuse from previous frame without allocating; do a linear scan to find a matching key.
- // (We avoid `drain()` here because it would eagerly move all entries.)
- if let Some(existing_key) = state
- .previous_frame
- .used_lines_by_hash
- .iter()
- .find(|key| {
- HashedCacheKeyRef {
- text_hash: key.text_hash,
- text_len: key.text_len,
- font_size: key.font_size,
- runs: key.runs.as_slice(),
- wrap_width: key.wrap_width,
- force_width: key.force_width,
- } == key_ref
- })
- .cloned()
- {
- if let Some((key, layout)) = state
- .previous_frame
- .lines_by_hash
- .remove_entry(&existing_key)
- {
- state
- .current_frame
- .lines_by_hash
- .insert(key.clone(), layout.clone());
- state.current_frame.used_lines_by_hash.push(key);
- return layout;
- }
- }
- drop(state);
-
- let text = materialize_text();
- let mut layout = self
- .platform_text_system
- .layout_line(&text, font_size, runs);
-
- if let Some(force_width) = force_width {
- apply_force_width_to_layout(&mut layout, force_width);
- }
-
- let key = Arc::new(HashedCacheKey {
- text_hash,
- text_len,
- font_size,
- runs: SmallVec::from(runs),
- wrap_width: None,
- force_width,
- });
- let layout = Arc::new(layout);
- let mut state = self.state.borrow_mut();
- state
- .current_frame
- .lines_by_hash
- .insert(key.clone(), layout.clone());
- state.current_frame.used_lines_by_hash.push(key);
- layout
- }
-
fn clear_if_font_generation_changed(&self) -> usize {
let font_generation = self.font_generation.load(Ordering::Acquire);
if self.cached_font_generation.load(Ordering::Acquire) == font_generation {
@@ -974,80 +772,12 @@ struct CacheKeyRef<'a> {
force_width: Option<Pixels>,
}
-#[derive(Clone, Debug)]
-struct HashedCacheKey {
- text_hash: u64,
- text_len: usize,
- font_size: Pixels,
- runs: SmallVec<[FontRun; 1]>,
- wrap_width: Option<Pixels>,
- force_width: Option<Pixels>,
-}
-
-#[derive(Copy, Clone)]
-struct HashedCacheKeyRef<'a> {
- text_hash: u64,
- text_len: usize,
- font_size: Pixels,
- runs: &'a [FontRun],
- wrap_width: Option<Pixels>,
- force_width: Option<Pixels>,
-}
-
impl PartialEq for dyn AsCacheKeyRef + '_ {
fn eq(&self, other: &dyn AsCacheKeyRef) -> bool {
self.as_cache_key_ref() == other.as_cache_key_ref()
}
}
-impl PartialEq for HashedCacheKey {
- fn eq(&self, other: &Self) -> bool {
- self.text_hash == other.text_hash
- && self.text_len == other.text_len
- && self.font_size == other.font_size
- && self.runs.as_slice() == other.runs.as_slice()
- && self.wrap_width == other.wrap_width
- && self.force_width == other.force_width
- }
-}
-
-impl Eq for HashedCacheKey {}
-
-impl Hash for HashedCacheKey {
- fn hash<H: Hasher>(&self, state: &mut H) {
- self.text_hash.hash(state);
- self.text_len.hash(state);
- self.font_size.hash(state);
- self.runs.as_slice().hash(state);
- self.wrap_width.hash(state);
- self.force_width.hash(state);
- }
-}
-
-impl PartialEq for HashedCacheKeyRef<'_> {
- fn eq(&self, other: &Self) -> bool {
- self.text_hash == other.text_hash
- && self.text_len == other.text_len
- && self.font_size == other.font_size
- && self.runs == other.runs
- && self.wrap_width == other.wrap_width
- && self.force_width == other.force_width
- }
-}
-
-impl Eq for HashedCacheKeyRef<'_> {}
-
-impl Hash for HashedCacheKeyRef<'_> {
- fn hash<H: Hasher>(&self, state: &mut H) {
- self.text_hash.hash(state);
- self.text_len.hash(state);
- self.font_size.hash(state);
- self.runs.hash(state);
- self.wrap_width.hash(state);
- self.force_width.hash(state);
- }
-}
-
impl Eq for dyn AsCacheKeyRef + '_ {}
impl Hash for dyn AsCacheKeyRef + '_ {