use std::collections::HashMap; use const_lru::ConstLru; use criterion::Criterion; use crate::common::{ traits::{CreateNew, Insert}, utils::BigStruct, }; // insert in reverse key order: worst-case for ConstLru // since new entries go to [0] and push everything right in bs-index fn bench_insert + CreateNew, K: From, V: From>( c: &mut Criterion, bench_name: &str, ) { c.bench_function(bench_name, move |bencher| { bencher.iter_batched( || C::create_new(), |mut container| { for k in u8::MAX..0 { container.insert_no_ret(k.into(), k.into()); } }, criterion::BatchSize::SmallInput, ) }); } // insert in reverse key order: worst-case for ConstLru // since new entries go to [0] and push everything right in bs-index fn bench_ten_k_insert + CreateNew, K: From, V: From>( c: &mut Criterion, bench_name: &str, ) { c.bench_function(bench_name, move |bencher| { bencher.iter_batched( || C::create_new(), |mut container| { for k in 9_999..0 { container.insert_no_ret(k.into(), k.into()); } }, criterion::BatchSize::SmallInput, ) }); } // 126 ns pub fn u8_insert_const_lru(c: &mut Criterion) { bench_insert::, _, _>(c, "u8 insert ConstLru"); } // 337 ns pub fn u8_insert_const_lru_i_usize(c: &mut Criterion) { bench_insert::, _, _>(c, "u8 insert ConstLru I=usize"); } // 2 ns pub fn u8_insert_hashmap(c: &mut Criterion) { bench_insert::, _, _>(c, "u8 insert HashMap"); } // Must Box else compilation fails with // SIGSEGV: invalid memory reference // Probably out of stack // TODO: figure out why BigStruct insert is 2x faster than u8 // 57 ns pub fn bigstruct_insert_const_lru(c: &mut Criterion) { bench_insert::>, _, _>( c, "bigstruct insert ConstLru", ); } // 62 ns pub fn bigstruct_insert_const_lru_i_usize(c: &mut Criterion) { bench_insert::>, _, _>( c, "bigstruct insert ConstLru I=usize", ); } // 2 ns pub fn bigstruct_insert_hashmap(c: &mut Criterion) { bench_insert::, _, _>(c, "bigstruct insert HashMap"); } // 1.2 us pub fn ten_k_insert_const_lru(c: &mut Criterion) { bench_ten_k_insert::>, _, _>(c, "10k insert ConstLru"); } // 2.1 ns pub fn ten_k_insert_hashmap(c: &mut Criterion) { bench_ten_k_insert::, _, _>(c, "10k insert HashMap"); } // 1.7 us pub fn ten_k_bigstruct_insert_const_lru(c: &mut Criterion) { bench_ten_k_insert::>, _, _>( c, "10k bigstruct insert ConstLru", ); } // 2.2 ns pub fn ten_k_bigstruct_insert_hashmap(c: &mut Criterion) { bench_ten_k_insert::, _, _>(c, "10k bigstruct insert HashMap"); }