diff --git a/lib/smol_str/CHANGELOG.md b/lib/smol_str/CHANGELOG.md index fb65d88ad191..b7da6d18a440 100644 --- a/lib/smol_str/CHANGELOG.md +++ b/lib/smol_str/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ## Unreleased +- Optimise `SmolStr::clone` 4-5x speedup inline, 0.5x heap (slow down). ## 0.3.4 - 2025-10-23 diff --git a/lib/smol_str/src/lib.rs b/lib/smol_str/src/lib.rs index a1d2c2f06744..91e287c58892 100644 --- a/lib/smol_str/src/lib.rs +++ b/lib/smol_str/src/lib.rs @@ -104,11 +104,19 @@ impl SmolStr { impl Clone for SmolStr { #[inline] fn clone(&self) -> Self { - if !self.is_heap_allocated() { - // SAFETY: We verified that the payload of `Repr` is a POD - return unsafe { core::ptr::read(self as *const SmolStr) }; + // hint for faster inline / slower heap clones + #[cold] + #[inline(never)] + fn cold_clone(v: &SmolStr) -> SmolStr { + SmolStr(v.0.clone()) } - Self(self.0.clone()) + + if self.is_heap_allocated() { + return cold_clone(self); + } + + // SAFETY: We verified that the payload of `Repr` is a POD + unsafe { core::ptr::read(self as *const SmolStr) } } }