Skip to content

perf(vm): preallocate map literal HashMap with known pair count 🗺️#157

Merged
timfennis merged 1 commit into
masterfrom
perf/makemap-with-capacity
May 24, 2026
Merged

perf(vm): preallocate map literal HashMap with known pair count 🗺️#157
timfennis merged 1 commit into
masterfrom
perf/makemap-with-capacity

Conversation

@timfennis
Copy link
Copy Markdown
Owner

Summary

  • OpCode::MakeMap knows the exact pair count at execution time, so HashMap::with_capacity(pairs) avoids the regrow/rehash churn HashMap::new() would incur as keys get inserted.
  • Adds benches/programs/map_literal_build.ndc so this path is actually exercised by cargo bench going forward — the existing map_ops bench only constructs %{}, which doesn't hit this code.

Measurement

Built release-with-debug binaries for master and this branch and compared with hyperfine --warmup 3 --runs 15 -N:

Benchmark base new ratio
map_literal_build (new) 90.7 ms 63.2 ms 1.44× faster ± 0.09
map_ops (existing, %{} only) 25.3 ms 23.5 ms within noise (1.07× ± 0.21)

The 32-entry-per-iteration literal in map_literal_build runs 50_000 times. Output (50000) matches on both binaries.

Test plan

  • cargo test --release passes (unchanged result set)
  • cargo fmt
  • Output of new bench identical between base and new binaries
  • hyperfine confirms speedup on populated map literals

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@timfennis timfennis merged commit 7afd812 into master May 24, 2026
1 check passed
@timfennis timfennis deleted the perf/makemap-with-capacity branch May 24, 2026 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant