19 releases (8 stable)

2.0.0 Mar 29, 2022
1.1.0 Mar 28, 2022
1.0.6 Nov 5, 2021
1.0.4 Jul 9, 2021
0.1.0 Mar 20, 2020

#39 in Data structures

Download history 33674/week @ 2022-04-18 37728/week @ 2022-04-25 43305/week @ 2022-05-02 42467/week @ 2022-05-09 37074/week @ 2022-05-16 29810/week @ 2022-05-23 29238/week @ 2022-05-30 28198/week @ 2022-06-06 33224/week @ 2022-06-13 28344/week @ 2022-06-20 34287/week @ 2022-06-27 32190/week @ 2022-07-04 34313/week @ 2022-07-11 31835/week @ 2022-07-18 33419/week @ 2022-07-25 37289/week @ 2022-08-01

140,491 downloads per month
Used in 130 crates (7 directly)

MIT/Apache

63KB
1.5K SLoC

KString

Key String: Optimized for map keys.

Crates Status

Background

Considerations:

  • Large maps
  • Most keys live and drop without being used in any other way
  • Most keys are relatively small (single to double digit bytes)
  • Keys are immutable
  • Allow zero-cost abstractions between structs and maps (e.g. no allocating when dealing with struct field names)

Ramifications:

  • Inline small strings rather than going to the heap.
  • Preserve &'static str across strings (KString), references (KStringRef), and lifetime abstractions (KStringCow) to avoid allocating for struct field names.
  • Use Box<str> rather than String to use less memory.

Features

  • max_inline: Instead of aligning the inline-string for performance (15 bytes + length on 64-bit), use the full width (22 bytes on 64-bit)
  • arc: Instead of using Box<str>, use Arc<str>. Note: allocations are fast enough that this can actually slow things down for small enough strings.

Alternatives

  • smol_str
    • Size of String
    • Always uses Arc instead of Box
    • Always inlines 22 bytes
    • Whitespace-only optimizations
  • smartstring
    • Size of String
    • Allows mutability at the cost of relying on implementation details of String
    • Always inlines 23 bytes
  • compact_str
    • Size of String
    • Always uses Arc instead of Box
    • Inlines 22-23 bytes, depending on implementation

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~110KB