#string #str #static #hashcons

hcstatic-str

compact storage for short static strings

2 releases

0.1.1 Jun 20, 2023
0.1.0 Jun 20, 2023

#2699 in Rust patterns

30 downloads per month

MIT license

7KB
145 lines

Hash Consed Static Short String Storage

This crate is for storing static short strings (up to 256 bytes) in as compact a way as possible. Instead of each string getting it's own allocation, and associated padding, header, etc, they are stored packed into 1 MiB allocations. The length is stored in the allocation, making the stack size of the Str type 1 word instead of the usual 2 for &str. Because the length is limited to 256 bytes only one extra byte is used in the heap allocation for the length.


lib.rs:

Global, permanent, packed, hashconsed, short string storage.

  • supports strings up to 256 bytes
  • derefs to a &str, but uses only 1 word on the stack and len + 1 bytes on the heap
  • the actual bytes are stored packed into 1 MiB allocations to avoid the overhead of lots of small mallocs
  • Copy!
  • hashconsed, the same &str will always produce a pointer to the same memory

CAN NEVER BE DEALLOCATED

Dependencies

~0.6–7MB
~16K SLoC