Crates.io | single_byte_hashmap |
lib.rs | single_byte_hashmap |
version | 0.1.2 |
source | src |
created_at | 2021-06-29 05:13:44.582789 |
updated_at | 2021-07-01 05:56:04.714757 |
description | A simple HashMap for storing singular bytes as keys |
homepage | https://github.com/billyb2/single_byte_hashmap-rs |
repository | https://github.com/billyb2/single_byte_hashmap-rs |
max_upload_size | |
id | 416020 |
size | 8,112 |
A very simple hashmap where the keys are just bytes
Essentially, this crate is just a hashmap, except the hashing function is literally just writing the value being hashed. The advantage to this is that the HashMap itself is very, very, very fast. I haven't done any benchmarks, but I'd guess that its probably hundreds of times faster than the standard library version, as well as literally any other implementation that isn't doing something extremely similar to this. At the very least, it definetly is faster.
The massive disadvantage, however, is that this sucks for literally any other input that isn't a singular byte. It definetly won't protect against collisions, and I'm unsure if it's vulnerable to any DoS attacks. Because of this, I added a little debug assertion that forces the HashMap to be used on single byte inputs, and nothing else.
I made this in like a half hour for another project I'm working on (for speed improvements), and thought that I might as well share this. Please don't use this crate if you're worried about security or having hashmaps with key values that are over a byte in size, since the only thing this crate is useful for is being fast.
The features of this crate just enable some features that are in HashBrown, since it's a faster implementation of the standard library's HashMap. The faster_hashmap
feature just uses HashBrown instead of the std HashMap, and is enabled by default.
Using this crate is almost exactly the same to the std HashMap (the only difference being you need to specify the Hasher)
use single_byte_hashmap::*;
let player_score: HashMap<u8, u16> = HashMap::with_capacity_and_hasher(256, BuildHasher::default());
player_score.insert(0, 2000);
player_score.insert(1, 3000);
for (player_id, score) in player_score.iter() {
println!("Player {} has a score of {}", player_id, score);
}