7 stable releases

1.1.1 Jan 31, 2024
1.1.0 Jan 27, 2024
1.0.3 Oct 26, 2023
1.0.2 Sep 25, 2023
1.0.0 Jul 30, 2023

#55 in Caching

Download history 15/week @ 2024-06-17 18/week @ 2024-06-24 9/week @ 2024-07-01 2/week @ 2024-07-08 24/week @ 2024-07-15 6/week @ 2024-07-22 56/week @ 2024-07-29 17/week @ 2024-08-05 14/week @ 2024-08-12 5/week @ 2024-08-19 24/week @ 2024-08-26 9/week @ 2024-09-02 11/week @ 2024-09-09 13/week @ 2024-09-16 23/week @ 2024-09-23 28/week @ 2024-09-30

77 downloads per month
Used in 3 crates (via lmfu)

MIT license

90KB
748 lines

String Pools / Strings Interning

Features

  • quick Deref<str> implementation - one pointer resolution, one comparison, and one pointer increment
  • thin PoolStr type - a pointer
  • The pool is deallocated only when every object referencing it have been dropped.
  • no_std, but alloc is required
  • thread-safe
  • Pool's Debug implementation allows you to see all of its strings
  • simple O(n / P) insertion/search, where P is Pool's const generic parameter

Example

# use {strpool::{Pool, PoolStr}, core::ops::Deref};

// you can reduce insertion/search complexity by raising this number,
// at the expense of more frequent allocations for small strings.
// strings are spread evenly into these subpools based on their hash.
// => must be a non-zero power of two.
const SUB_POOLS: usize = 1;

// no need for mutability, the pool uses atomic operations
let pool: Pool<SUB_POOLS> = Pool::new();

// use Pool::intern(&self, &str) to insert a string slice into the pool
// if the string was already present, that PoolStr will be reused.
let pool_string = pool.intern("Hello world!");

// you can obtain a &str with the Deref implementation
assert_eq!(pool_string.deref(), "Hello world!");
// Hash, Eq, Debug, Display are implemented as well.

// you can use Pool::find(&self, &str) to check if the pool contains a string
assert_eq!(pool.find("oh hi mark"), None);

// the empty string doesn't rely on a pool, it's always there
assert_eq!(pool.find(""), Some(PoolStr::empty()));

// See all interned strings via the Debug implementation
println!("{:#?}", pool);

Internal Memory Structure (Example)

memory.png

Dependencies

~165–365KB
~10K SLoC