/string-rosetta-rs

Comparison of Rust string types

Primary LanguageHTMLApache License 2.0Apache-2.0

Rust String Benchmarks

This repo tries to assess Rust string types.

We currently compare:

Name Size Heap Inline &'static str Mutable Unsafe Notes
String 24 bytes Y - N Y - Universal
Cow<'static, str> 24 bytes Y - Y N -
arcstr 8 bytes ? ? ? ? ? ?
compact_str 24 bytes Y 24 bytes N Y Y (miri, proptest, fuzz) Space optimized for Option<_>
ecow 16 bytes Y 15 bytes N Y Y (miri) O(1) clone unless mutated, Space optimized for Option<_>
flexstr 24 bytes Y 22 bytes Y N Y (miri) O(1) clone
hipstr 24 bytes ? 23 bytes ? ? ? ?
imstr 24 bytes ? ? ? ? ? ?
kstring 24 bytes Y 15 bytes Y N Optional (miri, proptest) Optional O(1) clone, optional 22 byte small string, Ref/Cow API for preserving &'static str
smartstring 24 bytes Y 23 bytes N Y Y (miri, proptest, fuzz)

Suggestions:

  • Generally, String
  • If you deal mostly with string literals but want some flexibility (like clap), generally you'll want Cow<'static, str>
  • If a profiler says your strings are a problem:
    • Try different crates and settings for that crate out with a profiler
    • O(1) clones are important when doing a lot of clones. For one-off allocations, they are slower.
    • For short-lived programs, look into string interning

Note: smol_str was removed in favor of ecow

Terms:

  • Heap: will store strings in heap-allocated memory
  • Inline: will store small-enough strings on the stack

Results

new summary: new

See more details

clone summary: clone

See more details

access summary: access

(smartstring is skipped due to how slow it is)

See more details

self_eq summary: self_eq

See more details

Special Thanks