JesperAxelsson / rust-intmap

Specialized hashmap for u64 keys
MIT License
30 stars 12 forks source link

Is hashmap in the standard library optimized #46

Closed yyy33 closed 3 months ago

yyy33 commented 7 months ago

I tried to test it over intmap


#![feature(test)]
use intmap::IntMap;
extern crate test;
use test::Bencher;
use std::collections::HashMap;

#[bench]
fn bench_int_map(b: &mut Bencher) {
    b.iter(|| {
        let mut map = IntMap::new();
        for i in 0..20_000 {
            map.insert(i, format!("item: {:?}", i));
        }
    });
}

#[bench]
fn bench_hash_map(b: &mut Bencher) {
    b.iter(|| {
        let mut map = HashMap::new();
        for i in 0..20_000 {
            map.insert(i, format!("item: {:?}", i));
        }
    });
}
❯ cargo bench
   Compiling test44 v0.1.0 (/home/yyy/test44)
    Finished `bench` profile [optimized] target(s) in 0.52s
     Running unittests src/main.rs (target/release/deps/test44-d293f15795ac1dd6)

running 2 tests
test bench_hash_map ... bench:   3,137,892 ns/iter (+/- 1,152,033)
test bench_int_map  ... bench:   7,604,677 ns/iter (+/- 2,871,172)

test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured; 0 filtered out; finished in 13.82s

❯ cargo bench
    Finished `bench` profile [optimized] target(s) in 0.03s
     Running unittests src/main.rs (target/release/deps/test44-d293f15795ac1dd6)

running 2 tests
test bench_hash_map ... bench:   3,020,571 ns/iter (+/- 2,373,478)
test bench_int_map  ... bench:  10,464,673 ns/iter (+/- 5,092,303)

test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured; 0 filtered out; finished in 9.71s
JesperAxelsson commented 3 months ago

Inserts without capacity set is slower in int_map than the standard hashmap implementation. Intmap is optimised for simplicity and a read heavy workload. A lot has changed since I wrote the IntMap and it might better to just use the standard hashmap with ahash instead.