Skip to main content

Module bench_loops

Module bench_loops 

Source
Expand description

Boilerplate-killer for perf examples. Most recipe benches follow one of two shapes:

  1. Keyed: generate N keys with SubMsLcg, time each op(&key). Common for hash maps, bloom filters, caches, dedup gates.
  2. Indexed: call op(i) for i in 0..N, time each. Common for sequential workloads (arena allocations, ring-buffer enqueues, fixed-size record reads).

Both shapes used to repeat ~6 lines per stage in every examples/perf_main.rs. These helpers collapse that to one call.

use subms::{SubMsLcg, SubMsPerfHarness, bench_keyed_op, bench_indexed_op};

let mut h = SubMsPerfHarness::new("my-recipe", "rust");
let mut data_structure = std::collections::HashMap::new();

// 50k keyed adds:
bench_keyed_op(&mut h, "add", 50_000, 0, |key| {
    data_structure.insert(key.to_string(), 1u32);
});

// 50k indexed ops:
bench_indexed_op(&mut h, "scan", 50_000, |i| {
    let _ = data_structure.get(&format!("k{i}"));
});

Functions§

bench_indexed_op
Run count timed invocations of op(i) for i in 0..count. Use for indexed sequential workloads (sequential ID generation, fixed record reads, arena allocations).
bench_keyed_op
Run count timed invocations of op against keys generated by a deterministic LCG seeded with seed. Each key is formatted as "k{u32}". The op closure receives the key string by reference.
bench_templated_op
Run count timed invocations of op against keys formatted from a caller-provided template. Useful when you specifically want negative-lookup keys (“miss-{i}”, “absent-{i}”) that don’t overlap the positive-lookup universe.