From 415d310e5260ae512b4e9e4981b44624db2ec9a3 Mon Sep 17 00:00:00 2001 From: Anthony Eid <56899983+Anthony-Eid@users.noreply.github.com> Date: Tue, 10 Nov 2026 23:00:03 +0000 Subject: [PATCH] gpui: Avoid copying and comparing the bytes of shared text (#66728) Two small changes that avoid touching the bytes of text that hasn't changed: - `SharedString` equality checks whether both sides point at the same bytes before comparing them. `SmolStr` compares heap-allocated and static strings byte by byte even when both are clones of the same allocation, which is common when a view clones the same label into its elements every frame. Strings of up to 23 bytes are stored inline and still use `SmolStr`'s comparison. `Hash` is now implemented by hand (clippy rejects deriving it alongside a manual `PartialEq`) and hashes exactly as before. - A `&'static str` text child (e.g. `div().child("Some label")`) is wrapped with `SharedString::new_static` instead of copied every frame, which allocated for strings longer than 23 bytes. Its comparisons now hit the pointer check above. Release Notes: - N/A - [GPUI] Avoid allocating for a SharedString in `imp Element for &'static str` --- crates/gpui/src/elements/text.rs | 2 +- crates/gpui_shared_string/gpui_shared_string.rs | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/crates/gpui/src/elements/text.rs b/crates/gpui/src/elements/text.rs index 4e095fbc232..1c9f660dceb 100644 --- a/crates/gpui/src/elements/text.rs +++ b/crates/gpui/src/elements/text.rs @@ -263,7 +263,7 @@ impl Element for &'static str { cx: &mut App, ) -> (LayoutId, Self::RequestLayoutState) { let state = TextLayout::default(); - let layout_id = state.layout(SharedString::from(*self), None, window, cx); + let layout_id = state.layout(SharedString::new_static(self), None, window, cx); (layout_id, state) } diff --git a/crates/gpui_shared_string/gpui_shared_string.rs b/crates/gpui_shared_string/gpui_shared_string.rs index bdb9e27453c..442851e822d 100644 --- a/crates/gpui_shared_string/gpui_shared_string.rs +++ b/crates/gpui_shared_string/gpui_shared_string.rs @@ -11,9 +11,22 @@ use smol_str::SmolStr; /// A shared string is an immutable string that can be cheaply cloned in GPUI /// tasks. Essentially an abstraction over an `Arc` and `&'static str`, /// currently backed by a [`SmolStr`]. -#[derive(Eq, PartialEq, PartialOrd, Ord, Hash, Clone)] +#[derive(Eq, PartialOrd, Ord, Clone)] pub struct SharedString(SmolStr); +impl PartialEq for SharedString { + fn eq(&self, other: &Self) -> bool { + let (this, other_str) = (self.as_str(), other.as_str()); + (this.as_ptr() == other_str.as_ptr() && this.len() == other_str.len()) || self.0 == other.0 + } +} + +impl std::hash::Hash for SharedString { + fn hash(&self, state: &mut H) { + self.0.hash(state); + } +} + impl std::ops::Deref for SharedString { type Target = str;