Lasso
A concurrent string interner that allows strings to be cached with a minimal memory footprint,
associating them with a unique key that can be used to retrieve them at any time. Lassos
allow O(1)
internment and resolution and can be turned into a ReadOnlyLasso
to allow for contention-free resolutions
with both key to str and str to key operations. It can also be turned into a ResolverLasso
with only
key to str operations for the lowest possible memory usage
Which Interner do I use?
No matter which interner you decide to use, you must start with a Lasso
, as that is the only way to intern strings and
the only way to get the other two interner types. As your program progresses though you may not need to intern strings anymore,
and at that point you may choose either a ReadOnlyLasso
or a ResolverLasso
. If you need to go from str to key you
should use a ReadOnlyLasso
, but anything else should use a ResolverLasso
.
Interner | Thread-safe | Intern String | key to str | str to key | Contention Free | Relative Memory Usage |
---|---|---|---|---|---|---|
Lasso |
:white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | Most |
ReadOnlyLasso |
:white_check_mark: | :x: | :white_check_mark: | :white_check_mark: | :white_check_mark: | Middle |
ResolverLasso |
:white_check_mark: | :x: | :white_check_mark: | :x: | :white_check_mark: | Least |
Example: Interning Strings across threads
use Lasso;
use ;
let lasso = new;
let hello = lasso.get_or_intern;
let l = clone;
let world = spawn
.join
.unwrap;
let world_2 = lasso.get_or_intern;
assert_eq!;
assert_eq!;
// These are the same because they interned the same string
assert_eq!;
assert_eq!;
Example: Resolving Strings
use Lasso;
let lasso = default;
let key = lasso.intern;
assert_eq!;
Example: Creating a ReadOnlyLasso
use Lasso;
use ;
let lasso = default;
let key = lasso.intern;
// Can be used for resolving strings with zero contention, but not for interning new ones
let read_only_lasso = new;
let lasso = clone;
spawn;
assert_eq!;
Example: Creating a ResolverLasso
use Lasso;
use ;
let lasso = default;
let key = lasso.intern;
// Can be used for resolving strings with zero contention and the lowest possible memory consumption,
// but not for interning new ones
let resolver_lasso = new;
let lasso = clone;
spawn;
assert_eq!;
Cargo Features
default
- By default theahasher
feature is enabledahasher
- Useahash::RandomState
as the default hasher for extra speed, without this then std'sRandomState
will be used