Skip to content

Commit 0a53f56

Browse files
marius-bughiuclaude
andcommitted
perf(bench): pre-size the HashSet baseline in SparseSetBenchmark Add for a fair comparison
SparseSet's universe is mandatory, so `new SparseSet(universe)` inherently pre-allocates its sparse array and pays no sparse resize on Add, whereas the `new HashSet<int>()` baseline paid full incremental-rehash growth — an unfair per-add comparison. Give the baseline the matching capacity hint (`new HashSet<int>(ItemCount)`, as the Setup / Remove paths already do) so the Add category measures per-add cost rather than the baseline's rehash overhead. Addresses a Copilot review comment on PR #288. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent f6a68a6 commit 0a53f56

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

src/Celerity.Benchmarks/SparseSetBenchmark.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ public void Setup()
4646
[BenchmarkCategory("Add")]
4747
public void HashSet_Add()
4848
{
49-
var set = new HashSet<int>();
49+
// Pre-sized to keep the per-add comparison fair: SparseSet's universe is
50+
// mandatory, so `new SparseSet(universe)` inherently pre-allocates its sparse
51+
// array (no sparse resize on Add). The baseline is given the matching capacity
52+
// hint (ItemCount, as the Setup / Remove paths already do) so this measures
53+
// per-add cost rather than HashSet's incremental-rehash overhead.
54+
var set = new HashSet<int>(ItemCount);
5055
foreach (var key in keys)
5156
set.Add(key);
5257
}

0 commit comments

Comments
 (0)