TL;DR
intern-mint is an implementation of byte slice interning.
About
Slice interning is a memory management technique that stores identical slices once in a slice pool.
This can potentially save memory and avoid allocations in environments where data is repetitive.
Technical details
Slices are kept as Arc<[u8]>s using the triomphe crate for a smaller footprint.
The Arcs are then stored in a global static pool implemented as a dumbed-down version of DashMap.
The pool consists of N shards (dependent on available_parallelism) of hashbrown hash-tables, sharded by the slices' hashes, to avoid locking the entire table for each lookup.
When a slice is dropped, the total reference count is checked, and the slice is removed from the pool if needed.
Interned and BorrowedInterned
Interned type is the main type offered by this crate, responsible for interning slices.
There is also &BorrowedInterned to pass around instead of cloning Interned instances when not needed,
and in order to avoid passing &Interned which will require double-dereference to access the data.
Examples
Same data will be held in the same address
use Interned;
let a = new;
let b = new;
assert_eq!;
&BorrowedInterned can be used with hash-maps
Note that the pointer is being used for hashing and comparing (see Hash and PartialEq trait implementations)
as opposed to hashing and comparing the actual data - because the pointers are unique for the same data as long as it "lives" in memory
use ;
let map = from_iter;
let key = new;
assert_eq!;
let borrowed_key: &BorrowedInterned = &key;
assert_eq!;
&BorrowedInterned can be used with btree-maps
use ;
let map = from_iter;
let key = new;
assert_eq!;
let borrowed_key: &BorrowedInterned = &key;
assert_eq!;
Additional features
The following features are available:
bstrto add some type conversions, and theDebugandDisplaytraits by using the bstr crate - enabled by defaultserdeto add theSerializeandDeserializetraits provided by the serde crate - disabled by defaultdatabufto add theEncodeandDecodetraits provided by the databuf crate - disabled by default