Crates.io | sparse_set_container |
lib.rs | sparse_set_container |
version | 1.1.1 |
source | src |
created_at | 2024-05-25 13:22:07.177645 |
updated_at | 2024-06-22 16:14:19.552257 |
description | A container based on sparse set. Stable keys, O(1) lookup, cache-friendly iterations, and no hashing. |
homepage | |
repository | https://github.com/gameraccoon/sparse_set |
max_upload_size | |
id | 1251965 |
size | 74,052 |
A container based on a sparse set.
It is useful if you want a container with performance close to Vec but you also want to safely store the indexes to the elements (so that they are not invalidated on removals).
E.g. you have a list of elements in UI that the user can add and remove, but you want to refer to the elements of that list from somewhere else.
Add this to your Cargo.toml:
[dependencies]
sparse_set_container = "1.1"
An array-like container based on sparse set implementation that allows O(1) access to elements without hashing and allows cache-friendly iterations.
Operation | SparseSet | Vec |
---|---|---|
push | O(1) | O(1) |
lookup | O(1) | O(1) |
size/len | O(1) | O(1) |
remove | O(n) | O(n) |
swap_remove | O(1) | O(1) |
For iterating over the elements SparseSet exposes an iterator over an internal Vec with values, which is as efficient as iterating over a Vec directly.
Differences to Vec:
4*sizeof(usize)
bytes on top of the size of the element itself
2^(sizeof(usize)*8)
removals the memory consumption will also grow by 2*sizeof(usize)
extern crate sparse_set_container;
use sparse_set_container::SparseSet;
fn main() {
let mut elements = SparseSet::new();
elements.push("1");
let key2 = elements.push("2");
elements.push("3");
elements.remove(key2);
elements.push("4");
if !elements.contains(key2) {
println!("Value 2 is not in the container");
}
// Prints 1 3 4
for v in elements.values() {
print!("{} ", v);
}
// Prints 1 3 4
for k in elements.keys() {
print!("{} ", elements.get(k).unwrap());
}
}
The values captured to illustrate the difference between this SparseSet container implementation, Vec, and standard HashMap:
Benchmark | SparseSet<String> |
Vec<String> |
HashMap<i32, String> |
---|---|---|---|
Create empty | 0 ns ±0 | 0 ns ±0 | 1 ns ±0 |
Create with capacity | 17 ns ±0 | 16 ns ±0 | 32 ns ±1 |
Push 100 elements | 3,254 ns ±14 | 2,553 ns ±23 | 5,493 ns ±85 |
With capacity push 100 | 3,286 ns ±30 | 3,156 ns ±106 | 4,388 ns ±21 |
Lookup 100 elements | 88 ns ±2 | 39 ns ±14 | 464 ns ±3 |
Iterate over 100 elements | 30 ns ±0 | 30 ns ±0 | 41 ns ±1 |
Clone with 100 elements | 2,184 ns ±23 | 2,109 ns ±4 | 1,490 ns ±32 |
Clone 100 and remove 10 | 3,055 ns ±107 | 2,364 ns ±97 | 1,692 ns ±145 |
Clone 100 and swap_remove 10 | 2,475 ns ±119 | 2,193 ns ±67 | N/A |
To run the benchmark on your machine, execute cargo run --example bench --release
Or to build this table you can run python tools/collect_benchmark_table.py
and then find the results in bench_table.md
Licensed under the MIT license: http://opensource.org/licenses/MIT