Skip to content

fix(bench): use val\<n\> instead of lit(n) for literal match dispatch#26

Merged
sentomk merged 8 commits intomainfrom
feat/on-compile-time-accel
May 1, 2026
Merged

fix(bench): use val\<n\> instead of lit(n) for literal match dispatch#26
sentomk merged 8 commits intomainfrom
feat/on-compile-time-accel

Conversation

@sentomk
Copy link
Copy Markdown
Owner

@sentomk sentomk commented May 1, 2026

Summary

Fix LiteralMatch 16/32/64 benchmarks that were 3-20x slower than if-else/switch because ptn::lit(n) runtime literal patterns fall back to O(n) linear scan instead of O(1) jump-table dispatch.

Changes

  • bench/bench_suite.cpp: changed PTN_RT_LIT_CASE(n) from ptn::lit(n) to ptn::val<n>
  • ci.yml: reverted chart filter to _LiteralMatch/ to keep chart clean

Results

Benchmark Before After
LiteralMatch32 10.7 ns 3.61 ns
LiteralMatch64 21.4 ns 7.01 ns

Checklist

  • Tests added or updated when behavior changed
  • Documentation updated when public API or behavior changed
  • Commit messages follow CONTRIBUTING.md
  • PR targets main and passes required CI

sentomk and others added 8 commits April 30, 2026 20:19
Problem
- Literal patterns with density below 1/4 fell back to O(n) linear scan.
- An intermediate tier with relaxed density (1/8) can provide O(1) array
  dispatch for patterns like 10 cases spanning 0-100.

Implementation
- Added k_runtime_literal_dense_dispatch_max_cases=256 / _max_span=512 /
  _max_density=8 / _min_cases=4 constants.
- Added is_runtime_literal_dense_dispatch_enabled trait.
- Added use_runtime_dense_table field to static_literal_dispatch_metadata.
- Added can_use_runtime_literal_dense_dispatch to case_sequence_ir and
  lowering_analysis.
- New literal_runtime_dense_dispatch_plan struct and dispatch_plan_for_kind
  specialization.
- Updated dispatch_plan_selector priority: static_dense -> runtime_dense
  -> literal_linear.
- Extended eval_cases_with_dispatch_plan to route runtime_dense plans to
  the same eval_cases_impl_static_literal_dispatch evaluator.

Tests
- Added runtime bench suite: 15 sparse literal values (density ~0.15)
  comparing PatterniaPipe / IfElse / Switch.
- Added compile-time TU ct_bench_lit_rdense.cpp exercising the tier at
  compile time via constexpr + static_assert.

Notes
- No DSL or public API surface changes.
Problem
- LiteralMatch 32/64 used ptn::lit(n) which creates runtime literal
  patterns invisible to the dense dispatch machinery.
- These fell back to O(n) linear scan (10.7 ns / 21.4 ns) instead
  of O(1) jump-table dispatch.

Implementation
- Changed PTN_RT_LIT_CASE(n) from ptn::lit(n) to ptn::val\<n\>.
- ptn::val\<n\> creates static_literal_pattern with compile-time
  visible values, enabling static_literal_dense dispatch.
- Reverted CI chart filter from _LiteralMatch to _LiteralMatch/
  to avoid overwhelming the chart with too many variants.

Tests
- LiteralMatch32: 10.7 ns -> 3.61 ns (3x improvement)
- LiteralMatch64: 21.4 ns -> 7.01 ns (3x improvement)
@codecov
Copy link
Copy Markdown

codecov Bot commented May 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@sentomk sentomk merged commit ae3b606 into main May 1, 2026
21 checks passed
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