mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-12-27 16:07:46 +00:00
Merge pull request #21017 from alexheretic/faster-inline-clones
Optimise `SmolStr::clone` 4-5x speedup inline, 0.5x heap (slow down)
This commit is contained in:
commit
813c7d43db
@ -1,6 +1,7 @@
|
||||
# Changelog
|
||||
|
||||
## Unreleased
|
||||
- Optimise `SmolStr::clone` 4-5x speedup inline, 0.5x heap (slow down).
|
||||
|
||||
## 0.3.4 - 2025-10-23
|
||||
|
||||
|
||||
@ -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) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user