Skip to content

Commit

Permalink
feat: add #[repr(u64)] to Repr to optimize clone cost (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWhiteWu committed Dec 23, 2023
1 parent b381d40 commit 1e0a9b2
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 99 deletions.
53 changes: 23 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "faststr"
version = "0.2.12"
version = "0.2.13"
authors = ["Volo Team <volo@cloudwego.io>"]
edition = "2021"
description = "Faststr is a string library that reduces the cost of clone."
Expand Down
94 changes: 29 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,118 +61,82 @@ $ cargo bench

### AARCH64

#### M1Max

```
empty faststr time: [19.315 ns 19.345 ns 19.377 ns]

empty string time: [2.2097 ns 2.2145 ns 2.2194 ns]

static faststr time: [19.483 ns 19.598 ns 19.739 ns]

inline faststr time: [20.447 ns 20.476 ns 20.507 ns]

string hello world time: [17.215 ns 17.239 ns 17.263 ns]

512B faststr time: [23.883 ns 23.922 ns 23.965 ns]

512B string time: [50.733 ns 51.360 ns 52.041 ns]

4096B faststr time: [23.893 ns 23.959 ns 24.033 ns]

4096B string time: [78.323 ns 79.565 ns 80.830 ns]

16384B faststr time: [23.829 ns 23.885 ns 23.952 ns]

16384B string time: [395.83 ns 402.46 ns 408.51 ns]

65536B faststr time: [23.934 ns 24.002 ns 24.071 ns]

65536B string time: [1.3142 µs 1.3377 µs 1.3606 µs]

524288B faststr time: [23.881 ns 23.926 ns 23.976 ns]

524288B string time: [8.8109 µs 8.8577 µs 8.9024 µs]

1048576B faststr time: [23.968 ns 24.032 ns 24.094 ns]

1048576B string time: [18.424 µs 18.534 µs 18.646 µs]
```

#### M3Max

```
empty faststr time: [16.178 ns 16.189 ns 16.201 ns]
empty faststr time: [2.0188 ns 2.0271 ns 2.0356 ns]

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

static faststr time: [16.310 ns 16.325 ns 16.341 ns]
static faststr time: [2.0458 ns 2.0589 ns 2.0709 ns]

inline faststr time: [17.024 ns 17.040 ns 17.056 ns]
inline faststr time: [2.2270 ns 2.2332 ns 2.2399 ns]

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

512B faststr time: [16.771 ns 16.790 ns 16.810 ns]
512B faststr time: [3.8373 ns 3.8454 ns 3.8540 ns]

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

4096B faststr time: [16.785 ns 16.811 ns 16.840 ns]
4096B faststr time: [3.8205 ns 3.8260 ns 3.8317 ns]

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

16384B faststr time: [16.796 ns 16.819 ns 16.843 ns]
16384B faststr time: [3.8191 ns 3.8246 ns 3.8306 ns]

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

65536B faststr time: [16.968 ns 17.011 ns 17.062 ns]
65536B faststr time: [3.8169 ns 3.8221 ns 3.8277 ns]

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

524288B faststr time: [16.806 ns 16.834 ns 16.868 ns]
524288B faststr time: [3.8140 ns 3.8178 ns 3.8219 ns]

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

1048576B faststr time: [16.823 ns 16.849 ns 16.880 ns]
1048576B faststr time: [3.8235 ns 3.8290 ns 3.8348 ns]

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

### AMD EPYC 7Y83
### amd64

#### AMD EPYC 7Y83

```
empty faststr time: [42.724 ns 42.728 ns 42.732 ns]
empty faststr time: [4.3325 ns 4.3330 ns 4.3335 ns]

empty string time: [4.6490 ns 4.6494 ns 4.6499 ns]
empty string time: [4.6413 ns 4.6422 ns 4.6434 ns]

static faststr time: [42.519 ns 42.525 ns 42.532 ns]
static faststr time: [4.3328 ns 4.3333 ns 4.3339 ns]

inline faststr time: [43.446 ns 43.450 ns 43.454 ns]
inline faststr time: [4.6567 ns 4.6580 ns 4.6593 ns]

string hello world time: [12.385 ns 12.385 ns 12.387 ns]
string hello world time: [12.897 ns 12.929 ns 12.954 ns]

512B faststr time: [42.232 ns 42.238 ns 42.244 ns]
512B faststr time: [4.4218 ns 4.4253 ns 4.4291 ns]

512B string time: [15.822 ns 15.846 ns 15.894 ns]
512B string time: [16.087 ns 16.094 ns 16.105 ns]

4096B faststr time: [41.741 ns 41.918 ns 42.069 ns]
4096B faststr time: [4.4066 ns 4.4099 ns 4.4141 ns]

4096B string time: [84.492 ns 84.668 ns 84.839 ns]
4096B string time: [96.905 ns 97.401 ns 97.879 ns]

16384B faststr time: [42.245 ns 42.250 ns 42.255 ns]
16384B faststr time: [4.4150 ns 4.4277 ns 4.4414 ns]

16384B string time: [225.36 ns 225.42 ns 225.47 ns]
16384B string time: [229.25 ns 229.30 ns 229.34 ns]

65536B faststr time: [41.987 ns 42.087 ns 42.166 ns]
65536B faststr time: [4.4562 ns 4.4623 ns 4.4690 ns]

65536B string time: [1.3212 µs 1.3215 µs 1.3219 µs]
65536B string time: [1.3325 µs 1.3328 µs 1.3332 µs]

524288B faststr time: [42.272 ns 42.277 ns 42.283 ns]
524288B faststr time: [4.4167 ns 4.4240 ns 4.4326 ns]

524288B string time: [14.373 µs 14.380 µs 14.388 µs]
524288B string time: [18.268 µs 18.277 µs 18.287 µs]

1048576B faststr time: [42.279 ns 42.287 ns 42.295 ns]
1048576B faststr time: [4.4275 ns 4.4385 ns 4.4494 ns]

1048576B string time: [27.995 µs 28.015 µs 28.038 µs]
1048576B string time: [32.839 µs 33.777 µs 34.554 µs]
```

## Related Projects
Expand Down
5 changes: 5 additions & 0 deletions benches/faststr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ fn criterion_benchmark(c: &mut Criterion) {

let s = FastStr::from("Hello, world!");
c.bench_function("static faststr", |b| b.iter(|| black_box(s.clone())));
#[allow(deprecated)]
let s = FastStr::new_inline("Hello, world!");
c.bench_function("inline faststr", |b| b.iter(|| black_box(s.clone())));
let s = String::from("Hello, world!");
c.bench_function("string hello world", |b| b.iter(|| black_box(s.clone())));

for size in [512, 4 * 1024, 16 * 1024, 64 * 1024, 512 * 1024, 1024 * 1024] {
let s = FastStr::from("a".repeat(size));
let _s1 = black_box(s.clone());
let _s2 = black_box(s.clone());
c.bench_function(format!("{}B faststr", size).as_str(), |b| {
b.iter(|| black_box(s.clone()))
});
drop(_s1);
drop(_s2);
let s = String::from("a".repeat(size));
c.bench_function(format!("{}B string", size).as_str(), |b| {
b.iter(|| black_box(s.clone()))
Expand Down
14 changes: 11 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ impl FastStr {
Self(Repr::new(text))
}

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

const INLINE_CAP: usize = 38;
const INLINE_CAP: usize = 30;

#[derive(Clone)]
#[repr(u64)]
enum Repr {
Empty,
Bytes(Bytes),
Expand Down

0 comments on commit 1e0a9b2

Please sign in to comment.