Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/billyb2/single_byte_hashmap-rs

A very simple hashmap for storing single bytes
https://github.com/billyb2/single_byte_hashmap-rs

Last synced: about 1 month ago
JSON representation

A very simple hashmap for storing single bytes

Awesome Lists containing this project

README

        

# Single Byte Hashmap
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](https://github.com/rust-lang/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.

## Usage
Using this crate is almost exactly the same to the std HashMap (the only difference being you need to specify the Hasher)

```rust
use single_byte_hashmap::*;

let player_score: HashMap = 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);

}

```