Crate internship [] [src]

Interned string and bytes for rust.

What is interning?

Interning is a method to store exactly one copy of immutable data.

Imagine your program holds lots of string values, mostly same value in it, and does not mutate them at all. If you use String to store them, lots of memories are wasted just for storing identical texts.

Interning efficiently eliminate this problem by managing global pool of cache, in the case above the type of the pool can be HashSet<Rc<str>>. When you need a new owned string, first you should lookup global pool for it. If desired string is found then use it. If not, just create a new one and put them also to the pool.

What does this library provide?

This crate exposes Intern<T> type correspond to Rc<T> but guaranteed to be unique over its value within thread and provide fast O(1) comparison and hashing.

Example

use internship::IStr;

let foo = IStr::new("foo"); // type is IStr
let foo2 = IStr::new("foo"); // reuse foo's buffer

let mut map = HashMap::new();
map.insert(IStr::new("key"), 42);
assert_eq!(map.get(&IStr::new("key")), Some(&42));

Why should I use IStr over Rc<str>?

IStr has some advantages over Rc<str>

  1. Space efficient

As only single allocation is happen per unique value, you can even spam IStr::new() without worrying about memory bloat.

  1. O(1) equality check

As only one copy of unique value can be exist, comparing two IStr can be done with just single pointer comparison instead comparing entire contents of strings.

  1. O(1) hash calculation

Again, as only one copy of unique value can be exist, its allocated memory address can represent underlying value so calculating hash over its pointer makes perfect sense to hash IStr. Now you can perform blazingly-fast hashmap lookup with arbitrary string key!

Structs

Intern

Interned data

Type Definitions

IBytes
ICStr
IOsStr
IPath
IStr