5 releases
0.1.4 | Sep 9, 2023 |
---|---|
0.1.3 | Sep 9, 2023 |
0.1.2 | Sep 9, 2023 |
0.1.1 | Sep 6, 2023 |
0.1.0 | Sep 6, 2023 |
#475 in Text processing
97 downloads per month
18KB
261 lines
strloin
strloin
gives you copy-on-write
(cow) slices of a string.
If the provided ranges form a single contiguous region, then you'll get back a
borrowed slice of the string. Otherwise, you'll get back an owned concatenation
of each range.
use strloin::Strloin;
let strloin = Strloin::new("hello world");
assert_eq!(strloin.from_ranges(&[0..5]), "hello"); // borrowed
assert_eq!(strloin.from_ranges(&[0..5, 5..11]), "hello world"); // borrowed
assert_eq!(strloin.from_ranges(&[0..5, 6..11]), "helloworld"); // owned
Note that this crate is intended for cases where borrowing is far more common
than cloning. If cloning is common, then it's likely that the performance
overhead, much less the cognitive overhead, is too expensive and you should
consider unconditionally cloning. Your mileage will vary. But, on a real-world
text parser where 85% of from_ranges
resulted in a borrow, switching from
always cloning to conditionally cloning with strloin had the following impact:
Benchmark 1: always-clone
Time (mean ± σ): 1.259 s ± 0.089 s [User: 0.062 s, System: 0.063 s]
Range (min … max): 1.082 s … 1.367 s 10 runs
Benchmark 2: strloin-slices
Time (mean ± σ): 394.7 ms ± 40.0 ms [User: 49.5 ms, System: 50.7 ms]
Range (min … max): 310.7 ms … 452.0 ms 10 runs
Benchmark 3: strloin-ranges
Time (mean ± σ): 376.5 ms ± 36.1 ms [User: 45.5 ms, System: 56.5 ms]
Range (min … max): 324.7 ms … 441.2 ms 10 runs
Summary
'strloin-ranges' ran
1.05 ± 0.15 times faster than 'strloin-slices'
3.34 ± 0.40 times faster than 'always-clone'
Optional features
beef
- Swap out thestd::borrow::Cow
implementation forbeef::lean::Cow
. The performance difference in my use case was just noise, but it may serve you better.
Dependencies
~10KB