Skip to content
This repository was archived by the owner on Nov 9, 2025. It is now read-only.

Commit 79daa40

Browse files
authored
Merge branch 'master' into faster-1-ascii-replace
2 parents 5093249 + b3ac1b5 commit 79daa40

4 files changed

Lines changed: 32 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- Optimise `StrExt::replace_smolstr`, `StrExt::replacen_smolstr` for single ascii replace,
66
~3x speedup inline & heap.
7+
- Optimise `StrExt::to_ascii_lowercase_smolstr`, `StrExt::to_ascii_uppercase_smolstr`
8+
~2x speedup inline, ~4-22x for heap.
79

810
## 0.3.2 - 2024-10-23
911

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ rustdoc-args = ["--cfg", "docsrs"]
1212
all-features = true
1313

1414
[dependencies]
15-
serde = { version = "1.0", optional = true, default-features = false }
15+
serde_core = { version = "1.0.220", optional = true, default-features = false }
1616
borsh = { version = "1.4.0", optional = true, default-features = false }
1717
arbitrary = { version = "1.3", optional = true }
1818

@@ -23,4 +23,5 @@ serde = { version = "1.0", features = ["derive"] }
2323

2424
[features]
2525
default = ["std"]
26-
std = ["serde?/std", "borsh?/std"]
26+
std = ["serde_core?/std", "borsh?/std"]
27+
serde = ["dep:serde_core"]

src/lib.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,12 +644,36 @@ impl StrExt for str {
644644

645645
#[inline]
646646
fn to_ascii_lowercase_smolstr(&self) -> SmolStr {
647-
from_char_iter(self.chars().map(|c| c.to_ascii_lowercase()))
647+
let len = self.len();
648+
if len <= INLINE_CAP {
649+
let mut buf = [0u8; INLINE_CAP];
650+
buf[..len].copy_from_slice(self.as_bytes());
651+
buf[..len].make_ascii_lowercase();
652+
SmolStr(Repr::Inline {
653+
// SAFETY: `len` is in bounds
654+
len: unsafe { InlineSize::transmute_from_u8(len as u8) },
655+
buf,
656+
})
657+
} else {
658+
self.to_ascii_lowercase().into()
659+
}
648660
}
649661

650662
#[inline]
651663
fn to_ascii_uppercase_smolstr(&self) -> SmolStr {
652-
from_char_iter(self.chars().map(|c| c.to_ascii_uppercase()))
664+
let len = self.len();
665+
if len <= INLINE_CAP {
666+
let mut buf = [0u8; INLINE_CAP];
667+
buf[..len].copy_from_slice(self.as_bytes());
668+
buf[..len].make_ascii_uppercase();
669+
SmolStr(Repr::Inline {
670+
// SAFETY: `len` is in bounds
671+
len: unsafe { InlineSize::transmute_from_u8(len as u8) },
672+
buf,
673+
})
674+
} else {
675+
self.to_ascii_uppercase().into()
676+
}
653677
}
654678

655679
#[inline]

src/serde.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use alloc::{string::String, vec::Vec};
22
use core::fmt;
33

44
use serde::de::{Deserializer, Error, Unexpected, Visitor};
5+
use serde_core as serde;
56

67
use crate::SmolStr;
78

0 commit comments

Comments
 (0)