Files
zed/crates/gpui_shared_string/gpui_shared_string.rs
T
Anthony Eid 415d310e52 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`
2026-11-10 23:00:03 +00:00

224 lines
4.8 KiB
Rust

use std::{
borrow::{Borrow, Cow},
iter,
sync::Arc,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
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<str>` and `&'static str`,
/// currently backed by a [`SmolStr`].
#[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<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
impl std::ops::Deref for SharedString {
type Target = str;
fn deref(&self) -> &Self::Target {
self.0.as_str()
}
}
impl SharedString {
/// Creates a static [`SharedString`] from a `&'static str`.
pub const fn new_static(str: &'static str) -> Self {
Self(SmolStr::new_static(str))
}
/// Creates a [`SharedString`].
pub fn new(str: impl AsRef<str>) -> Self {
SharedString(SmolStr::new(str))
}
/// Get a &str from the underlying string.
pub fn as_str(&self) -> &str {
&self.0
}
}
impl JsonSchema for SharedString {
fn inline_schema() -> bool {
String::inline_schema()
}
fn schema_name() -> Cow<'static, str> {
String::schema_name()
}
fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
String::json_schema(generator)
}
}
impl Default for SharedString {
fn default() -> Self {
Self::new_static("")
}
}
impl AsRef<str> for SharedString {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Borrow<str> for SharedString {
fn borrow(&self) -> &str {
self.as_ref()
}
}
impl std::fmt::Debug for SharedString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl std::fmt::Display for SharedString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.as_str())
}
}
impl PartialEq<String> for SharedString {
fn eq(&self, other: &String) -> bool {
self.as_ref() == other
}
}
impl PartialEq<SharedString> for String {
fn eq(&self, other: &SharedString) -> bool {
self == other.as_ref()
}
}
impl PartialEq<str> for SharedString {
fn eq(&self, other: &str) -> bool {
self.as_ref() == other
}
}
impl<'a> PartialEq<&'a str> for SharedString {
fn eq(&self, other: &&'a str) -> bool {
self.as_ref() == *other
}
}
impl From<&SharedString> for SharedString {
#[inline]
fn from(s: &SharedString) -> SharedString {
s.clone()
}
}
impl From<&str> for SharedString {
#[inline]
fn from(s: &str) -> SharedString {
SharedString(SmolStr::from(s))
}
}
impl From<char> for SharedString {
#[inline]
fn from(c: char) -> SharedString {
SharedString(SmolStr::from_iter(iter::once(c)))
}
}
impl From<&mut str> for SharedString {
#[inline]
fn from(s: &mut str) -> SharedString {
SharedString(SmolStr::from(s))
}
}
impl From<&String> for SharedString {
#[inline]
fn from(s: &String) -> SharedString {
SharedString(SmolStr::from(s))
}
}
impl From<String> for SharedString {
#[inline(always)]
fn from(text: String) -> Self {
SharedString(SmolStr::from(text))
}
}
impl From<Box<str>> for SharedString {
#[inline]
fn from(s: Box<str>) -> SharedString {
SharedString(SmolStr::from(s))
}
}
impl From<Arc<str>> for SharedString {
#[inline]
fn from(s: Arc<str>) -> SharedString {
SharedString(SmolStr::from(s))
}
}
impl From<&Arc<str>> for SharedString {
#[inline]
fn from(s: &Arc<str>) -> SharedString {
SharedString(SmolStr::from(s.clone()))
}
}
impl<'a> From<Cow<'a, str>> for SharedString {
#[inline]
fn from(s: Cow<'a, str>) -> SharedString {
SharedString(SmolStr::from(s))
}
}
impl From<SharedString> for Arc<str> {
#[inline(always)]
fn from(text: SharedString) -> Self {
text.0.into()
}
}
impl From<SharedString> for String {
#[inline(always)]
fn from(text: SharedString) -> Self {
text.0.into()
}
}
impl Serialize for SharedString {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.as_ref())
}
}
impl<'de> Deserialize<'de> for SharedString {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
SmolStr::deserialize(deserializer).map(SharedString)
}
}