sarlacc

Crates.iosarlacc
lib.rssarlacc
version0.1.4
created_at2025-05-18 16:59:29.622654+00
updated_at2025-07-26 18:20:04.035922+00
descriptionThread-safe lock-free interning of data
homepage
repositoryhttps://gitlab.com/hrovnyak/sarlacc
max_upload_size
id1678773
size85,760
Henry Rovnyak (Xendergo)

documentation

README

Sarlacc

Some say that it takes 1000 years for your data to be freed...

Sarlacc is a crate for easy interning of data in Rust. Interning is the process of deduplicating equal values in memory, allowing them to be hashed and checked for equality through quick and efficient pointer comparison.

This crate aims to have feature parity with the popular internment crate, however interning is implemented using a Ctrie, which is a lock-free hash set that synchronizes using atomics. The internment crate simply hides everything behind a global mutex.

Now you may be wondering... Why would you want to leak your entire memory stick BLAZINGLY FAST 🚀🚀🚀???

  • This crate supports interning inside of arenas, which can be dropped when no longer needed
  • Interning values that you expect to already be interned or checking whether a value is already interned does not involve a global lock and the operation does not leak memory
  • Locks always have the issue that a thread could be pre-empted while holding the lock, leading to all other threads stalling
  • Locks often involve OS syscalls

Todos:

  • Handle hash collisions with separate chaining instead of panicking (Note that the entire 64 bit hash has to collide for there to be an issue)
  • Implement deletion from the Ctrie and ArcIntern functionality

Example

let a = Intern::from_ref("Hello");
let b = Intern::from_ref("world");
assert_ne!(a, b);
println!("{a}, {b}");

let also_a = Intern::from_ref("Hello");
assert_eq!(a, also_a);
assert!(ptr::eq(&*a, &*also_a));

Note: This crate uses a ton of unsafe code and is my way of learning unsafe and atomics; it's probably a fairly risky dependency to use

Commit count: 22

cargo fmt