#interning #string-interning #symbols #garbage-collection

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

#459 in Memory management

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
~70K SLoC