#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

#364 in Memory management

Download history 3/week @ 2024-07-22 300/week @ 2024-07-29 359/week @ 2024-08-05 92/week @ 2024-08-12 34/week @ 2024-09-16 10/week @ 2024-09-23 26/week @ 2024-09-30

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