Unnamed repository; edit this file 'description' to name the repository.
Add `PartialEq` shortcut for `ptr_eq` strings
This first compares the `Repr` before falling back to actually comparing the raw `as_str` itself.
In some micro-benchmarks, this speeds up inline and heap string comparisons when equal by ~70%.
There is a tiny hit in the non-equal case however. It is also noteworthy that the assembly generated for `Repr` is horrible,
and looks like its above the inlining threshold now.
| -rw-r--r-- | lib/smol_str/src/lib.rs | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/lib/smol_str/src/lib.rs b/lib/smol_str/src/lib.rs index 192f972353..375a4a5b6b 100644 --- a/lib/smol_str/src/lib.rs +++ b/lib/smol_str/src/lib.rs @@ -180,7 +180,7 @@ impl Deref for SmolStr { impl PartialEq<SmolStr> for SmolStr { fn eq(&self, other: &SmolStr) -> bool { - self.as_str() == other.as_str() + self.0.ptr_eq(&other.0) || self.as_str() == other.as_str() } } @@ -424,7 +424,7 @@ const _: () = { assert!(WS.as_bytes()[N_NEWLINES] == b' '); }; -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq)] #[repr(u8)] enum InlineSize { _V0 = 0, @@ -536,6 +536,24 @@ impl Repr { } } } + + fn ptr_eq(&self, other: &Self) -> bool { + match (self, other) { + (Self::Heap(l0), Self::Heap(r0)) => Arc::ptr_eq(l0, r0), + (Self::Static(l0), Self::Static(r0)) => core::ptr::eq(l0, r0), + ( + Self::Inline { + len: l_len, + buf: l_buf, + }, + Self::Inline { + len: r_len, + buf: r_buf, + }, + ) => l_len == r_len && l_buf == r_buf, + _ => false, + } + } } /// Convert value to [`SmolStr`] using [`fmt::Display`], potentially without allocating. |