Skip to content
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

- Optimise `StrExt::to_ascii_lowercase_smolstr`, `StrExt::to_ascii_uppercase_smolstr`
~2x speedup inline, ~4-22x for heap.

## 0.3.2 - 2024-10-23

- Fix `SmolStrBuilder::push` incorrectly padding null bytes when spilling onto the heap on a
Expand Down
28 changes: 26 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,12 +644,36 @@ impl StrExt for str {

#[inline]
fn to_ascii_lowercase_smolstr(&self) -> SmolStr {
from_char_iter(self.chars().map(|c| c.to_ascii_lowercase()))
let len = self.len();
if len <= INLINE_CAP {
let mut buf = [0u8; INLINE_CAP];
buf[..len].copy_from_slice(self.as_bytes());
buf[..len].make_ascii_lowercase();
SmolStr(Repr::Inline {
// SAFETY: `len` is in bounds
len: unsafe { InlineSize::transmute_from_u8(len as u8) },
buf,
})
} else {
self.to_ascii_lowercase().into()
}
}

#[inline]
fn to_ascii_uppercase_smolstr(&self) -> SmolStr {
from_char_iter(self.chars().map(|c| c.to_ascii_uppercase()))
let len = self.len();
if len <= INLINE_CAP {
let mut buf = [0u8; INLINE_CAP];
buf[..len].copy_from_slice(self.as_bytes());
buf[..len].make_ascii_uppercase();
SmolStr(Repr::Inline {
// SAFETY: `len` is in bounds
len: unsafe { InlineSize::transmute_from_u8(len as u8) },
buf,
})
} else {
self.to_ascii_uppercase().into()
}
}

#[inline]
Expand Down