2 releases
0.1.1 | Aug 27, 2023 |
---|---|
0.1.0 | Jul 28, 2022 |
#2612 in Rust patterns
63 downloads per month
Used in netsblox-vm
19KB
289 lines
bin-pool
is a small crate for interning binary slices.
A type called BinPool
is provided which represents a collection of binary slices;
however, the content is highly optimized for space by allowing slices to overlap in memory.
The data is backed by a smaller subset of "large" slices that have been added previously.
Note that interning is not a cheap operation, and in the worst case every backing slice must be examined when adding a new slice. Interning is only recommended when memory is very limited, or for storage optimization in areas where time is not critical (e.g., compiler output).
Example
let mut b = BinPool::new();
b.add(b"hello world".as_slice()); // add first slice - backed by itself
b.add(b"hello".as_slice()); // add another slice - backed by first
b.add(b"world".as_slice()); // add another slice - backed by first
assert_eq!(b.bytes(), 21); // 21 bytes stored
assert_eq!(b.backing_bytes(), 11); // but only 11 bytes needed to represent them
b.add(b"hello world!".as_slice()); // add another slice - becomes the backer for others
assert_eq!(b.bytes(), 33); // now 33 bytes stored
assert_eq!(b.backing_bytes(), 12); // but only 12 bytes to represent them
no_std
bin-pool
supports building in no_std
environments by disabling default features.
Note that the alloc
crate is required.
[dependencies]
bin-pool = { version = "...", default-features = false }