Rust String Parsing Benchmarks

This repo tries to assess Rust string types.

We currently compare:

Name Heap Inline &'static str Mutable Unsafe Notes
String Y N Y Universal
Cow<'static, str> Y Y N
compact_str Y 24 bytes N Y Y (miri, proptest, fuzz)
flexstr Y 22 bytes Y N Y (miri) O(1) clone
kstring 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 Y 23 bytes N Y Y (miri, proptest, fuzz)
smol_str Y 22 bytes N N Y (miri, proptest) O(1) clone, Whitespace storage optimizations

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

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

See more details

Special Thanks

GitHub

View Github