plugmem-arena 0.1.4

Flat byte-pool storage structures for plugmem: sharded sorted arena, blob heap, chunked lists, interner.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! Reproducible cross-runtime micro-benchmark — **zero dependencies**.
//!
//! Compares the arena against the standard library's collections of the
//! same *workload class* (ordered map / lookup map / flat sorted array) on:
//!
//! - insert throughput and **per-insert tail latency** (p50 / p99 / max —
//!   this is where page splits, tree rebalancing, hash-table rehashes and
//!   O(n) array shifts become visible);
//! - point lookup and ordered scan throughput;
//! - retained and **peak** memory, and the number of **allocator calls**
//!   during a build (measured with a counting global allocator).
//!
//! Runs identically on native and inside WebAssembly runtimes; the corpus
//! size is the first CLI argument (default 100000), so the same binary
//! measures both the design center and the scale ceiling:
//!
//! ```text
//! # native
//! cargo run --release --example bench_repro            # 100k
//! cargo run --release --example bench_repro -- 1000000 # 1M
//!
//! # wasm (either runtime)
//! cargo build --release --example bench_repro --target wasm32-wasip1
//! wasmtime run target/wasm32-wasip1/release/examples/bench_repro.wasm 1000000
//! wasmer run  target/wasm32-wasip1/release/examples/bench_repro.wasm -- 1000000
//! ```
//!
//! The whole matrix (every structure x every runtime x both sizes) is
//! scripted by `cargo run --release -p plugmem-bench-matrix`.
//!
//! Methodology: fixed workload (seeded xorshift keys — identical streams
//! everywhere), throughput = best of 3, latency percentiles from a separate
//! single instrumented pass (each insert bracketed by `Instant::now()`; the
//! clock-call overhead inflates p50 by a constant, spikes are unaffected —
//! compare percentiles *within* a runtime, not across). No criterion here
//! on purpose: this example must not pull a single crate, so the wasm
//! builds stay trivial and the numbers are easy to audit.

use std::alloc::{GlobalAlloc, Layout, System};
use std::collections::{BTreeMap, HashMap};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Instant;

use plugmem_arena::{
    Arena, ArenaCfg, BlobHeap, BlobHeapCfg, BlobId, ChunkPool, ChunkPoolCfg, Interner, ListHandle,
    ShardMode, Slot, TermId, key,
};

// --- counting allocator ---------------------------------------------------

/// Global allocator wrapper counting live bytes, peak bytes and calls.
struct Counting;

static LIVE: AtomicUsize = AtomicUsize::new(0);
static PEAK: AtomicUsize = AtomicUsize::new(0);
static CALLS: AtomicUsize = AtomicUsize::new(0);

// SAFETY: delegates verbatim to `System`; the counters are metadata only.
unsafe impl GlobalAlloc for Counting {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        CALLS.fetch_add(1, Ordering::Relaxed);
        let live = LIVE.fetch_add(layout.size(), Ordering::Relaxed) + layout.size();
        PEAK.fetch_max(live, Ordering::Relaxed);
        unsafe { System.alloc(layout) }
    }
    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        LIVE.fetch_sub(layout.size(), Ordering::Relaxed);
        unsafe { System.dealloc(ptr, layout) }
    }
    unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
        CALLS.fetch_add(1, Ordering::Relaxed);
        let live = LIVE.fetch_add(new_size, Ordering::Relaxed) + new_size - layout.size();
        LIVE.fetch_sub(layout.size(), Ordering::Relaxed);
        PEAK.fetch_max(live, Ordering::Relaxed);
        unsafe { System.realloc(ptr, layout, new_size) }
    }
}

#[global_allocator]
static ALLOC: Counting = Counting;

/// Allocator activity of one measured build.
struct AllocStat {
    calls: usize,
    peak: usize,
    retained: usize,
}

impl AllocStat {
    /// Rescales the byte counters so the per-N report reads per stored
    /// item instead (used when stored items != stream length, e.g. the
    /// interner keeps only distinct terms). Call counts stay as measured.
    fn scaled(self, items: usize, n: usize) -> Self {
        // u128: `bytes * n` overflows a 32-bit usize on wasm32.
        let per = |v: usize| (v as u128 * n as u128 / items.max(1) as u128) as usize;
        Self {
            calls: self.calls,
            peak: per(self.peak),
            retained: per(self.retained),
        }
    }
}

/// Runs `f` with the allocator counters scoped to it.
fn with_alloc_stats<T>(f: impl FnOnce() -> T) -> (T, AllocStat) {
    let live0 = LIVE.load(Ordering::Relaxed);
    let calls0 = CALLS.load(Ordering::Relaxed);
    PEAK.store(live0, Ordering::Relaxed);
    let out = f();
    (
        out,
        AllocStat {
            calls: CALLS.load(Ordering::Relaxed) - calls0,
            peak: PEAK.load(Ordering::Relaxed) - live0,
            retained: LIVE.load(Ordering::Relaxed) - live0,
        },
    )
}

// --- workload -------------------------------------------------------------

/// The same 16-byte record class as `benches/storage.rs`: 12-byte composite
/// key, 4-byte payload.
#[derive(Clone, Copy)]
struct Rec16 {
    hi: u64,
    lo: u32,
    val: u32,
}

impl Slot for Rec16 {
    const SIZE: usize = 16;
    const KEY_LEN: usize = 12;
    fn write(&self, out: &mut [u8]) {
        key::write_pair(out, self.hi, self.lo);
        out[12..16].copy_from_slice(&self.val.to_be_bytes());
    }
    fn read(bytes: &[u8]) -> Self {
        let (hi, lo) = key::read_pair(bytes);
        Self {
            hi,
            lo,
            val: u32::from_be_bytes(bytes[12..16].try_into().unwrap()),
        }
    }
}

fn rec(hi: u64) -> Rec16 {
    Rec16 { hi, lo: 0, val: 1 }
}

fn rec_key(hi: u64) -> [u8; 12] {
    let mut k = [0u8; 12];
    key::write_pair(&mut k, hi, 0);
    k
}

fn xorshift(seed: u64) -> impl FnMut() -> u64 {
    let mut s = seed;
    move || {
        s ^= s << 13;
        s ^= s >> 7;
        s ^= s << 17;
        s
    }
}

const LOOKUPS: usize = 10_000;
const SCAN: usize = 1_000;
const REPS: usize = 3;

/// Runs `f` REPS times, returns the best wall time in nanoseconds.
fn best_ns<T>(mut f: impl FnMut() -> T) -> u64 {
    let mut best = u64::MAX;
    for _ in 0..REPS {
        let t = Instant::now();
        let out = f();
        let ns = t.elapsed().as_nanos() as u64;
        std::hint::black_box(out);
        best = best.min(ns);
    }
    best
}

/// Per-insert latency percentiles of one instrumented build pass.
struct Tail {
    p50: u64,
    p99: u64,
    max: u64,
}

/// Times `insert(key)` for every key individually and extracts percentiles.
fn tail_of(keys: &[u64], mut insert: impl FnMut(u64)) -> Tail {
    let mut ns: Vec<u64> = Vec::with_capacity(keys.len());
    for &k in keys {
        let t = Instant::now();
        insert(k);
        ns.push(t.elapsed().as_nanos() as u64);
    }
    ns.sort_unstable();
    Tail {
        p50: ns[ns.len() / 2],
        p99: ns[ns.len() * 99 / 100],
        max: *ns.last().unwrap(),
    }
}

// --- reporting ------------------------------------------------------------

/// One structure's measured metrics; `None` = not applicable to its class.
struct Row {
    name: &'static str,
    insert_ns: Option<u64>,
    get_ns: Option<u64>,
    scan_ns: Option<u64>,
    tail: Option<Tail>,
    alloc: Option<AllocStat>,
}

fn emit(n: usize, rows: &[Row]) {
    println!(
        "bench_repro: N={n}, lookups={LOOKUPS}, scan={SCAN}, reps={REPS} (best kept), \
         latency pass: single, clock overhead included in p50"
    );
    // Machine-readable: one `#M <structure> <metric> <value>` line each.
    for r in rows {
        let mut m: Vec<(&str, f64)> = Vec::new();
        if let Some(v) = r.insert_ns {
            m.push(("insert_ns", v as f64 / n as f64));
        }
        if let Some(v) = r.get_ns {
            m.push(("get_ns", v as f64 / LOOKUPS as f64));
        }
        if let Some(v) = r.scan_ns {
            m.push(("scan_ns", v as f64 / SCAN as f64));
        }
        if let Some(t) = &r.tail {
            m.push(("ins_p50", t.p50 as f64));
            m.push(("ins_p99", t.p99 as f64));
            m.push(("ins_max", t.max as f64));
        }
        if let Some(a) = &r.alloc {
            m.push(("mem_b", a.retained as f64 / n as f64));
            m.push(("mem_peak_b", a.peak as f64 / n as f64));
            m.push(("allocs", a.calls as f64));
        }
        for (metric, value) in m {
            println!("#M\t{}\t{metric}\t{value:.1}", r.name);
        }
    }
}

// --- main -----------------------------------------------------------------

fn main() {
    let n: usize = std::env::args()
        .nth(1)
        .and_then(|a| a.parse().ok())
        .unwrap_or(100_000);
    let keys: Vec<u64> = {
        let mut rng = xorshift(0xB0B5_1234_5678_9ABC);
        (0..n).map(|_| rng()).collect()
    };
    let hit_keys: Vec<u64> = keys.iter().step_by(n / LOOKUPS).copied().collect();
    let mut sorted = keys.clone();
    sorted.sort_unstable();
    let (scan_from, scan_to) = (sorted[n / 2], sorted[n / 2 + SCAN]);

    let mut rows = Vec::new();

    // --- plugmem arena, Ordered (the ordered-map class) -------------------
    {
        let build = || {
            let mut a = Arena::<Rec16>::new(ArenaCfg::new(1024, ShardMode::Ordered)).unwrap();
            for &k in &keys {
                a.insert(&rec(k)).unwrap();
            }
            a
        };
        let insert_ns = best_ns(build);
        let (arena, alloc) = with_alloc_stats(build);
        let mut tailed = Arena::<Rec16>::new(ArenaCfg::new(1024, ShardMode::Ordered)).unwrap();
        let tail = tail_of(&keys, |k| {
            tailed.insert(&rec(k)).unwrap();
        });
        let get_ns = best_ns(|| {
            let mut acc = 0u32;
            for &k in &hit_keys {
                acc += u32::from(arena.contains(&rec_key(k)));
            }
            acc
        });
        let scan_ns = best_ns(|| {
            let mut acc = 0u64;
            for r in arena.range(&rec_key(scan_from), &rec_key(scan_to)) {
                acc += u64::from(r.val);
            }
            acc
        });
        rows.push(Row {
            name: "plugmem Arena (Ordered)",
            insert_ns: Some(insert_ns),
            get_ns: Some(get_ns),
            scan_ns: Some(scan_ns),
            tail: Some(tail),
            alloc: Some(alloc),
        });
    }

    // --- plugmem arena, Uniform (the lookup-map class) --------------------
    {
        let build = || {
            let mut a = Arena::<Rec16>::new(ArenaCfg::new(1024, ShardMode::Uniform)).unwrap();
            for &k in &keys {
                a.insert(&rec(k)).unwrap();
            }
            a
        };
        let insert_ns = best_ns(build);
        let (arena, alloc) = with_alloc_stats(build);
        let get_ns = best_ns(|| {
            let mut acc = 0u32;
            for &k in &hit_keys {
                acc += u32::from(arena.contains(&rec_key(k)));
            }
            acc
        });
        rows.push(Row {
            name: "plugmem Arena (Uniform)",
            insert_ns: Some(insert_ns),
            get_ns: Some(get_ns),
            scan_ns: None,
            tail: None,
            alloc: Some(alloc),
        });
    }

    // --- BTreeMap (std ordered map — the same class) ----------------------
    {
        let build = || {
            let mut m: BTreeMap<u64, u32> = BTreeMap::new();
            for &k in &keys {
                m.insert(k, 1);
            }
            m
        };
        let insert_ns = best_ns(build);
        let (map, alloc) = with_alloc_stats(build);
        let mut tailed: BTreeMap<u64, u32> = BTreeMap::new();
        let tail = tail_of(&keys, |k| {
            tailed.insert(k, 1);
        });
        let get_ns = best_ns(|| {
            let mut acc = 0u32;
            for &k in &hit_keys {
                acc += u32::from(map.contains_key(&k));
            }
            acc
        });
        let scan_ns = best_ns(|| {
            let mut acc = 0u64;
            for (_, v) in map.range(scan_from..scan_to) {
                acc += u64::from(*v);
            }
            acc
        });
        rows.push(Row {
            name: "std BTreeMap",
            insert_ns: Some(insert_ns),
            get_ns: Some(get_ns),
            scan_ns: Some(scan_ns),
            tail: Some(tail),
            alloc: Some(alloc),
        });
    }

    // --- HashMap (std lookup map — no ordering) ---------------------------
    {
        let build = || {
            let mut m: HashMap<u64, u32> = HashMap::new();
            for &k in &keys {
                m.insert(k, 1);
            }
            m
        };
        let insert_ns = best_ns(build);
        let (map, alloc) = with_alloc_stats(build);
        let mut tailed: HashMap<u64, u32> = HashMap::new();
        let tail = tail_of(&keys, |k| {
            tailed.insert(k, 1);
        });
        let get_ns = best_ns(|| {
            let mut acc = 0u32;
            for &k in &hit_keys {
                acc += u32::from(map.contains_key(&k));
            }
            acc
        });
        rows.push(Row {
            name: "std HashMap",
            insert_ns: Some(insert_ns),
            get_ns: Some(get_ns),
            scan_ns: None,
            tail: Some(tail),
            alloc: Some(alloc),
        });
    }

    // --- sorted Vec, bulk build (flat baseline's best case) ---------------
    {
        let build = || {
            let mut v: Vec<(u64, u32)> = keys.iter().map(|&k| (k, 1)).collect();
            v.sort_unstable_by_key(|&(k, _)| k);
            v
        };
        let insert_ns = best_ns(build);
        let (vec, alloc) = with_alloc_stats(build);
        let get_ns = best_ns(|| {
            let mut acc = 0u32;
            for &k in &hit_keys {
                acc += u32::from(vec.binary_search_by_key(&k, |&(k, _)| k).is_ok());
            }
            acc
        });
        let scan_ns = best_ns(|| {
            let start = vec.partition_point(|&(k, _)| k < scan_from);
            let mut acc = 0u64;
            for &(k, v) in &vec[start..] {
                if k >= scan_to {
                    break;
                }
                acc += u64::from(v);
            }
            acc
        });
        rows.push(Row {
            name: "sorted Vec (bulk)",
            insert_ns: Some(insert_ns),
            get_ns: Some(get_ns),
            scan_ns: Some(scan_ns),
            tail: None,
            alloc: Some(alloc),
        });
    }

    // --- sorted Vec, incremental (why the arena exists) -------------------
    // Keeping a flat array sorted while inserting is O(n) per insert: the
    // whole tail shifts. One instrumented pass doubles as the throughput
    // measurement (a full best-of-3 would take minutes). Skipped at 1M —
    // the quadratic build would run for hours.
    if n <= 100_000 {
        let mut v: Vec<(u64, u32)> = Vec::new();
        let t = Instant::now();
        let tail = tail_of(&keys, |k| {
            let at = v.partition_point(|&(existing, _)| existing < k);
            v.insert(at, (k, 1));
        });
        let insert_ns = t.elapsed().as_nanos() as u64;
        std::hint::black_box(&v);
        rows.push(Row {
            name: "sorted Vec (incremental)",
            insert_ns: Some(insert_ns),
            get_ns: None,
            scan_ns: None,
            tail: Some(tail),
            alloc: None,
        });
    }

    // === companion structures vs their std-class baselines =================
    // Metrics reuse the same keys: insert_ns = push/intern (per stream
    // element), get_ns = get/resolve (per lookup), scan_ns = full iteration
    // (per element), mem/allocs = build stats. Memory is per *stored* item
    // (blobs / values / distinct terms).

    // --- BlobHeap vs Vec<Vec<u8>>: append-only blob storage ---------------
    {
        // Variable-length blobs, 16..=200 bytes (avg ~108), deterministic.
        let mut rng = xorshift(0x0B10_B0B5_0000_0001);
        let blobs: Vec<Vec<u8>> = (0..n)
            .map(|_| {
                let len = 16 + (rng() % 185) as usize;
                (0..len).map(|i| i as u8).collect()
            })
            .collect();
        let ids: Vec<u32> = (0..LOOKUPS).map(|_| (rng() % n as u64) as u32).collect();

        let build = || {
            let mut h = BlobHeap::new(BlobHeapCfg::new());
            for b in &blobs {
                h.push(b).unwrap();
            }
            h
        };
        let insert_ns = best_ns(build);
        let (heap, alloc) = with_alloc_stats(build);
        let get_ns = best_ns(|| {
            let mut acc = 0usize;
            for &id in &ids {
                acc += heap.get(BlobId(id)).len();
            }
            acc
        });
        rows.push(Row {
            name: "plugmem BlobHeap",
            insert_ns: Some(insert_ns),
            get_ns: Some(get_ns),
            scan_ns: None,
            tail: None,
            alloc: Some(alloc),
        });

        let build_std = || {
            let mut v: Vec<Vec<u8>> = Vec::new();
            for b in &blobs {
                v.push(b.clone()); // one heap allocation per blob
            }
            v
        };
        let insert_ns = best_ns(build_std);
        let (vecs, alloc) = with_alloc_stats(build_std);
        let get_ns = best_ns(|| {
            let mut acc = 0usize;
            for &id in &ids {
                acc += vecs[id as usize].len();
            }
            acc
        });
        rows.push(Row {
            name: "Vec<Vec<u8>> (blob baseline)",
            insert_ns: Some(insert_ns),
            get_ns: Some(get_ns),
            scan_ns: None,
            tail: None,
            alloc: Some(alloc),
        });
    }

    // --- ChunkPool vs one Vec<u8> per list: many small lists --------------
    {
        const LISTS: usize = 1024;
        let values: Vec<[u8; 8]> = keys.iter().map(|k| k.to_be_bytes()).collect();

        let build = || {
            let mut pool = ChunkPool::new(ChunkPoolCfg::new());
            let mut lists = vec![ListHandle::EMPTY; LISTS];
            for (i, v) in values.iter().enumerate() {
                pool.push(&mut lists[i % LISTS], v).unwrap();
            }
            (pool, lists)
        };
        let insert_ns = best_ns(build);
        let ((pool, lists), alloc) = with_alloc_stats(build);
        // Iteration sums every byte on both sides, so the chain-hop cost
        // of the pool is compared against a contiguous slice doing the
        // same work; the total is normalized to read per *value* after
        // emit's division by SCAN.
        let scan_total = best_ns(|| {
            let mut acc = 0usize;
            for list in &lists {
                for chunk in pool.iter(list) {
                    for &b in chunk {
                        acc += b as usize;
                    }
                }
            }
            acc
        });
        rows.push(Row {
            name: "plugmem ChunkPool",
            insert_ns: Some(insert_ns),
            get_ns: None,
            scan_ns: Some(scan_total * SCAN as u64 / n as u64),
            tail: None,
            alloc: Some(alloc),
        });

        let build_std = || {
            let mut lists: Vec<Vec<u8>> = vec![Vec::new(); LISTS];
            for (i, v) in values.iter().enumerate() {
                lists[i % LISTS].extend_from_slice(v);
            }
            lists
        };
        let insert_ns = best_ns(build_std);
        let (std_lists, alloc) = with_alloc_stats(build_std);
        let scan_total = best_ns(|| {
            let mut acc = 0usize;
            for list in &std_lists {
                for &b in list {
                    acc += b as usize;
                }
            }
            acc
        });
        rows.push(Row {
            name: "Vec<u8> per list (chunk baseline)",
            insert_ns: Some(insert_ns),
            get_ns: None,
            scan_ns: Some(scan_total * SCAN as u64 / n as u64),
            tail: None,
            alloc: Some(alloc),
        });
    }

    // --- Interner vs HashMap<String,u32> + Vec<String> --------------------
    {
        // Vocabulary of n/10 distinct terms; the stream draws uniformly, so
        // ~90% of intern calls are hits after warm-up (the realistic mix).
        let vocab = (n / 10).max(1);
        let mut rng = xorshift(0x1234_5678_9ABC_DEF0);
        let words: Vec<String> = (0..n)
            .map(|_| format!("term-{}", rng() % vocab as u64))
            .collect();

        let build = || {
            let mut it = Interner::new(BlobHeapCfg::new());
            for w in &words {
                it.intern(w).unwrap();
            }
            it
        };
        let insert_ns = best_ns(build);
        let (interner, alloc) = with_alloc_stats(build);
        let distinct = interner.len();
        let resolve_ids: Vec<u32> = (0..LOOKUPS).map(|i| (i % distinct) as u32).collect();
        let get_ns = best_ns(|| {
            let mut acc = 0usize;
            for &id in &resolve_ids {
                acc += interner.resolve(TermId(id)).len();
            }
            acc
        });
        rows.push(Row {
            name: "plugmem Interner",
            insert_ns: Some(insert_ns),
            get_ns: Some(get_ns),
            scan_ns: None,
            tail: None,
            alloc: Some(alloc.scaled(distinct, n)),
        });

        let build_std = || {
            let mut map: std::collections::HashMap<String, u32> = std::collections::HashMap::new();
            let mut names: Vec<String> = Vec::new();
            for w in &words {
                if !map.contains_key(w.as_str()) {
                    map.insert(w.clone(), names.len() as u32);
                    names.push(w.clone());
                }
            }
            (map, names)
        };
        let insert_ns = best_ns(build_std);
        let ((_, names), alloc) = with_alloc_stats(build_std);
        let get_ns = best_ns(|| {
            let mut acc = 0usize;
            for &id in &resolve_ids {
                acc += names[id as usize].len();
            }
            acc
        });
        rows.push(Row {
            name: "HashMap+Vec (intern baseline)",
            insert_ns: Some(insert_ns),
            get_ns: Some(get_ns),
            scan_ns: None,
            tail: None,
            alloc: Some(alloc.scaled(distinct, n)),
        });
    }

    emit(n, &rows);
}