Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/smol_str/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
- Optimise `SmolStr::clone` 4-5x speedup inline, 0.5x heap (slow down).

## 0.3.4 - 2025-10-23

Expand Down
16 changes: 12 additions & 4 deletions lib/smol_str/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
}

Expand Down