Skip to content

Commit 0631a66

Browse files
authored
feat: use more inline (#11)
1 parent 231882f commit 0631a66

File tree

3 files changed

+52
-58
lines changed

3 files changed

+52
-58
lines changed

Cargo.lock

Lines changed: 33 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ keywords = ["string", "str", "volo"]
1313

1414
[dependencies]
1515
bytes = "1"
16-
serde = { version = "1", optional = true, default_features = false }
16+
serde = { version = "1", optional = true, default-features = false }
1717
simdutf8 = { version = "0.1", features = ["aarch64_neon"] }
18-
redis = { version = "0.25", optional = true, default_features = false }
18+
redis = { version = "0.25", optional = true, default-features = false }
1919
itoa = { version = "1", optional = true }
2020

2121
[features]
@@ -26,7 +26,6 @@ serde-unsafe = ["serde"]
2626
redis = ["dep:redis", "itoa"]
2727
redis-unsafe = ["redis"]
2828

29-
3029
[dev-dependencies]
3130
static_assertions = { version = "1" }
3231
criterion = { version = "0.5", features = ["html_reports"] }

src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,27 @@ impl FastStr {
7878
/// Create a new `FastStr` from an `Arc<str>`.
7979
#[inline]
8080
pub fn from_arc_str(s: Arc<str>) -> Self {
81+
if Self::can_inline(&s) {
82+
return Self::new(s);
83+
}
8184
Self(Repr::from_arc_str(s))
8285
}
8386

8487
/// Create a new `FastStr` from a `String`.
8588
#[inline]
8689
pub fn from_string(s: String) -> Self {
90+
if Self::can_inline(&s) {
91+
return Self::new(s);
92+
}
8793
Self(Repr::from_string(s))
8894
}
8995

9096
/// Create a new `FastStr` from an `Arc<String>`.
9197
#[inline]
9298
pub fn from_arc_string(s: Arc<String>) -> Self {
99+
if Self::can_inline(&s) {
100+
return Self::new(s.as_str());
101+
}
93102
Self(Repr::from_arc_string(s))
94103
}
95104

@@ -110,6 +119,10 @@ impl FastStr {
110119
/// `b` must be valid UTF-8.
111120
#[inline]
112121
pub unsafe fn from_bytes_unchecked(b: Bytes) -> Self {
122+
let s = std::str::from_utf8_unchecked(&b);
123+
if Self::can_inline(s) {
124+
return Self::new(s);
125+
}
113126
Self(Repr::from_bytes_unchecked(b))
114127
}
115128

@@ -274,6 +287,10 @@ impl FastStr {
274287
}
275288
Self(Repr::Inline { len, buf })
276289
}
290+
291+
fn can_inline(s: &str) -> bool {
292+
s.len() <= INLINE_CAP
293+
}
277294
}
278295

279296
impl Default for FastStr {

0 commit comments

Comments
 (0)