#interning #string-interning

refuse-pool

A string interner utilizing the Refuse garbage collector

5 releases

0.0.6 Aug 11, 2024
0.0.5 Aug 10, 2024
0.0.4 Aug 2, 2024
0.0.3 May 3, 2024
0.0.2 Apr 3, 2024

#363 in Memory management

Download history 211/week @ 2024-07-28 315/week @ 2024-08-04 219/week @ 2024-08-11 9/week @ 2024-08-18 33/week @ 2024-09-15 10/week @ 2024-09-22 27/week @ 2024-09-29

388 downloads per month

MIT/Apache

125KB
2.5K SLoC

Garbage-collected "interned" strings.

Interning is a process of making many equal things share the same underlying resource. This crate introduces two types that are powered by the Refuse garbage collector:

  • RootString: A Root<String>-like type that ensures all instances of the same exact byte sequence refer to the same allocation.
  • RefString: A Ref<String> type that is a reference to a RootString.
use refuse::CollectionGuard;
use refuse_pool::{RefString, RootString};

let a = RootString::from("a");
let a_again = RootString::from(String::from("a"));

// Both a and a_again point to the same underlying storage.
assert_eq!(a.root_count(), 2);
// Comparing two RootStrings is cheap.
assert_eq!(a, a_again);

// a_ref can be used to gain a reference to a string,
// but only until the string is unreachable.
let a_ref = a.downgrade();

let mut guard = CollectionGuard::acquire();
assert_eq!(a_ref.load(&guard), Some("a"));

drop(a);
drop(a_again);
guard.collect();
assert_eq!(a_ref.load(&guard), None);

Dependencies

~3–8.5MB
~68K SLoC