Skip to content

About

Comparison of Rust string types

Resources

Stars

188 stars

Watchers

1 watching

Forks

Latest commit

 

History

171 Commits

Folders and files

Repository files navigation

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 ? ? ? ? ? ?
cold-string 8 bytes Y 8 bytes N N Y (miri, proptest) Optional O(1) clone, space optimized for Option<_>, compact variable-sized length encoding
compact_str 24 bytes Y 24 bytes Y 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 Y 23 bytes Y Y Y (miri) O(1) clone, O(1) substring
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
lean_string 16 bytes Y 16 bytes Y Y Y (miri, proptest) O(1) clone unless mutated, Space optimized for Option<_>
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

About

Comparison of Rust string types

Resources

Stars

188 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages