feat(optimize): add literal_runtime_dense dispatch tier#24
Merged
Conversation
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.
6eb65d2 to
8a35432
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add a
literal_runtime_densedispatch tier that sits betweenstatic_literal_dense(density >= 1/4) andliteral_linear(O(n) fallback). By relaxing the density threshold to 1/8, sparse literal patterns (e.g. 10 cases spanning 0-100) get O(1) array dispatch instead of linear scan. No DSL or public API surface changes.Changes
Core (
optimize.hpp,eval.hpp)k_runtime_literal_dense_dispatch_max_cases=256,_max_span=512,_max_density=8,_min_cases=4is_runtime_literal_dense_dispatch_enabledtraitstatic_literal_dispatch_metadatagainsuse_runtime_dense_tablefieldcase_sequence_ir/lowering_analysisexportcan_use_runtime_literal_dense_dispatchliteral_runtime_dense_dispatch_planstruct,dispatch_plan_for_kindspecializationdispatch_plan_selectorpriority: static_dense -> runtime_dense -> literal_lineareval_cases_with_dispatch_planroutes runtime_dense to the sameeval_cases_impl_static_literal_dispatchevaluatorBenchmarks (
bench/)PatterniaPipe/IfElse/Switch_LiteralMatchRDense(3 benchmarks)ct_bench_lit_rdense.cpp(constexpr + static_assert)Tests
Checklist
CONTRIBUTING.mdmainand passes required CI