Skip to content

Commit 1e0a9b2

Browse files
authored
feat: add #[repr(u64)] to Repr to optimize clone cost (volo-rs#6)
Thanks to @wfly1998 @0xd34d10cc
1 parent b381d40 commit 1e0a9b2

File tree

5 files changed

+69
-99
lines changed

5 files changed

+69
-99
lines changed

Cargo.lock

+23-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "faststr"
3-
version = "0.2.12"
3+
version = "0.2.13"
44
authors = ["Volo Team <[email protected]>"]
55
edition = "2021"
66
description = "Faststr is a string library that reduces the cost of clone."

README.md

+29-65
Original file line numberDiff line numberDiff line change
@@ -61,118 +61,82 @@ $ cargo bench
6161

6262
### AARCH64
6363

64-
#### M1Max
65-
66-
```
67-
empty faststr time: [19.315 ns 19.345 ns 19.377 ns]
68-
69-
empty string time: [2.2097 ns 2.2145 ns 2.2194 ns]
70-
71-
static faststr time: [19.483 ns 19.598 ns 19.739 ns]
72-
73-
inline faststr time: [20.447 ns 20.476 ns 20.507 ns]
74-
75-
string hello world time: [17.215 ns 17.239 ns 17.263 ns]
76-
77-
512B faststr time: [23.883 ns 23.922 ns 23.965 ns]
78-
79-
512B string time: [50.733 ns 51.360 ns 52.041 ns]
80-
81-
4096B faststr time: [23.893 ns 23.959 ns 24.033 ns]
82-
83-
4096B string time: [78.323 ns 79.565 ns 80.830 ns]
84-
85-
16384B faststr time: [23.829 ns 23.885 ns 23.952 ns]
86-
87-
16384B string time: [395.83 ns 402.46 ns 408.51 ns]
88-
89-
65536B faststr time: [23.934 ns 24.002 ns 24.071 ns]
90-
91-
65536B string time: [1.3142 µs 1.3377 µs 1.3606 µs]
92-
93-
524288B faststr time: [23.881 ns 23.926 ns 23.976 ns]
94-
95-
524288B string time: [8.8109 µs 8.8577 µs 8.9024 µs]
96-
97-
1048576B faststr time: [23.968 ns 24.032 ns 24.094 ns]
98-
99-
1048576B string time: [18.424 µs 18.534 µs 18.646 µs]
100-
```
101-
10264
#### M3Max
10365

10466
```
105-
empty faststr time: [16.178 ns 16.189 ns 16.201 ns]
67+
empty faststr time: [2.0188 ns 2.0271 ns 2.0356 ns]
10668

10769
empty string time: [2.1306 ns 2.1333 ns 2.1365 ns]
10870

109-
static faststr time: [16.310 ns 16.325 ns 16.341 ns]
71+
static faststr time: [2.0458 ns 2.0589 ns 2.0709 ns]
11072

111-
inline faststr time: [17.024 ns 17.040 ns 17.056 ns]
73+
inline faststr time: [2.2270 ns 2.2332 ns 2.2399 ns]
11274

11375
string hello world time: [12.553 ns 12.575 ns 12.597 ns]
11476

115-
512B faststr time: [16.771 ns 16.790 ns 16.810 ns]
77+
512B faststr time: [3.8373 ns 3.8454 ns 3.8540 ns]
11678

11779
512B string time: [36.895 ns 37.007 ns 37.121 ns]
11880

119-
4096B faststr time: [16.785 ns 16.811 ns 16.840 ns]
81+
4096B faststr time: [3.8205 ns 3.8260 ns 3.8317 ns]
12082

12183
4096B string time: [55.275 ns 55.355 ns 55.446 ns]
12284

123-
16384B faststr time: [16.796 ns 16.819 ns 16.843 ns]
85+
16384B faststr time: [3.8191 ns 3.8246 ns 3.8306 ns]
12486

12587
16384B string time: [338.18 ns 352.36 ns 365.02 ns]
12688

127-
65536B faststr time: [16.968 ns 17.011 ns 17.062 ns]
89+
65536B faststr time: [3.8169 ns 3.8221 ns 3.8277 ns]
12890

12991
65536B string time: [662.52 ns 663.75 ns 664.96 ns]
13092

131-
524288B faststr time: [16.806 ns 16.834 ns 16.868 ns]
93+
524288B faststr time: [3.8140 ns 3.8178 ns 3.8219 ns]
13294

13395
524288B string time: [6.2681 µs 6.2755 µs 6.2827 µs]
13496

135-
1048576B faststr time: [16.823 ns 16.849 ns 16.880 ns]
97+
1048576B faststr time: [3.8235 ns 3.8290 ns 3.8348 ns]
13698

13799
1048576B string time: [12.422 µs 12.438 µs 12.453 µs]
138100
```
139101

140-
### AMD EPYC 7Y83
102+
### amd64
103+
104+
#### AMD EPYC 7Y83
141105

142106
```
143-
empty faststr time: [42.724 ns 42.728 ns 42.732 ns]
107+
empty faststr time: [4.3325 ns 4.3330 ns 4.3335 ns]
144108

145-
empty string time: [4.6490 ns 4.6494 ns 4.6499 ns]
109+
empty string time: [4.6413 ns 4.6422 ns 4.6434 ns]
146110

147-
static faststr time: [42.519 ns 42.525 ns 42.532 ns]
111+
static faststr time: [4.3328 ns 4.3333 ns 4.3339 ns]
148112

149-
inline faststr time: [43.446 ns 43.450 ns 43.454 ns]
113+
inline faststr time: [4.6567 ns 4.6580 ns 4.6593 ns]
150114

151-
string hello world time: [12.385 ns 12.385 ns 12.387 ns]
115+
string hello world time: [12.897 ns 12.929 ns 12.954 ns]
152116

153-
512B faststr time: [42.232 ns 42.238 ns 42.244 ns]
117+
512B faststr time: [4.4218 ns 4.4253 ns 4.4291 ns]
154118

155-
512B string time: [15.822 ns 15.846 ns 15.894 ns]
119+
512B string time: [16.087 ns 16.094 ns 16.105 ns]
156120

157-
4096B faststr time: [41.741 ns 41.918 ns 42.069 ns]
121+
4096B faststr time: [4.4066 ns 4.4099 ns 4.4141 ns]
158122

159-
4096B string time: [84.492 ns 84.668 ns 84.839 ns]
123+
4096B string time: [96.905 ns 97.401 ns 97.879 ns]
160124

161-
16384B faststr time: [42.245 ns 42.250 ns 42.255 ns]
125+
16384B faststr time: [4.4150 ns 4.4277 ns 4.4414 ns]
162126

163-
16384B string time: [225.36 ns 225.42 ns 225.47 ns]
127+
16384B string time: [229.25 ns 229.30 ns 229.34 ns]
164128

165-
65536B faststr time: [41.987 ns 42.087 ns 42.166 ns]
129+
65536B faststr time: [4.4562 ns 4.4623 ns 4.4690 ns]
166130

167-
65536B string time: [1.3212 µs 1.3215 µs 1.3219 µs]
131+
65536B string time: [1.3325 µs 1.3328 µs 1.3332 µs]
168132

169-
524288B faststr time: [42.272 ns 42.277 ns 42.283 ns]
133+
524288B faststr time: [4.4167 ns 4.4240 ns 4.4326 ns]
170134

171-
524288B string time: [14.373 µs 14.380 µs 14.388 µs]
135+
524288B string time: [18.268 µs 18.277 µs 18.287 µs]
172136

173-
1048576B faststr time: [42.279 ns 42.287 ns 42.295 ns]
137+
1048576B faststr time: [4.4275 ns 4.4385 ns 4.4494 ns]
174138

175-
1048576B string time: [27.995 µs 28.015 µs 28.038 µs]
139+
1048576B string time: [32.839 µs 33.777 µs 34.554 µs]
176140
```
177141

178142
## Related Projects

benches/faststr.rs

+5
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@ fn criterion_benchmark(c: &mut Criterion) {
99

1010
let s = FastStr::from("Hello, world!");
1111
c.bench_function("static faststr", |b| b.iter(|| black_box(s.clone())));
12+
#[allow(deprecated)]
1213
let s = FastStr::new_inline("Hello, world!");
1314
c.bench_function("inline faststr", |b| b.iter(|| black_box(s.clone())));
1415
let s = String::from("Hello, world!");
1516
c.bench_function("string hello world", |b| b.iter(|| black_box(s.clone())));
1617

1718
for size in [512, 4 * 1024, 16 * 1024, 64 * 1024, 512 * 1024, 1024 * 1024] {
1819
let s = FastStr::from("a".repeat(size));
20+
let _s1 = black_box(s.clone());
21+
let _s2 = black_box(s.clone());
1922
c.bench_function(format!("{}B faststr", size).as_str(), |b| {
2023
b.iter(|| black_box(s.clone()))
2124
});
25+
drop(_s1);
26+
drop(_s2);
2227
let s = String::from("a".repeat(size));
2328
c.bench_function(format!("{}B string", size).as_str(), |b| {
2429
b.iter(|| black_box(s.clone()))

src/lib.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,17 @@ impl FastStr {
3434
Self(Repr::new(text))
3535
}
3636

37-
/// Create a new inline `FastStr` (up to 38 bytes long) from a string slice `s`.
37+
/// Create a new inline `FastStr` (up to 30 bytes long) from a string slice `s`.
3838
///
39-
/// This constructor panics if the length of `s` is greater than 38.
39+
/// This constructor panics if the length of `s` is greater than 30.
40+
///
41+
/// Note: the inline length is not guaranteed.
4042
#[inline]
43+
#[doc(hidden)]
44+
#[deprecated(
45+
since = "0.2.13",
46+
note = "The inline threshold is not stable. Please use `FastStr::new()` instead."
47+
)]
4148
pub const fn new_inline(s: &str) -> Self {
4249
if s.len() > INLINE_CAP {
4350
panic!("[FastStr] string is too long to inline");
@@ -496,9 +503,10 @@ impl From<Cow<'static, str>> for FastStr {
496503
}
497504
}
498505

499-
const INLINE_CAP: usize = 38;
506+
const INLINE_CAP: usize = 30;
500507

501508
#[derive(Clone)]
509+
#[repr(u64)]
502510
enum Repr {
503511
Empty,
504512
Bytes(Bytes),

0 commit comments

Comments
 (0)