Sort switch cases by value; coalesce identical-body cases#254
Open
AaronWebster wants to merge 1 commit into
Open
Sort switch cases by value; coalesce identical-body cases#254AaronWebster wants to merge 1 commit into
AaronWebster wants to merge 1 commit into
Conversation
Three composing changes to the optimized Ok() switch generator:
1. Case-label sort. Each switch arm's labels are sorted by the
underlying integer/enum value before emit. _case_sort_key()
returns the int for sorting. Sorted cases give older embedded
GCCs (the ones shipped with microblaze-elf and many bare-metal
arm-none-eabi toolchains) a better shot at emitting a dense
jump table rather than an if-ladder.
2. Identical-body coalescing. Cases whose rendered body text is
identical (same field set in the same order) are merged into a
single arm with multiple \`case X:\` labels. The C++ compiler
emits one body for the whole arm — a real text-size win once a
later PR (disjunction matching) starts producing such pairs.
3. Multi-field per case. When two conditional fields share a
discriminant + case value (\`if tag == 0: a\` and \`if tag == 0: b\`),
they're now bundled into the same case arm rather than the second
falling back to a separate if-statement. Each field's validation
becomes one line of the case body.
The ok_method_switch_case template becomes ok_method_switch_arm,
taking pre-formatted \${case_labels} and \${case_body} strings.
Single-label single-field arms render identically to the old
template, so golden churn is limited to the f0_copy field in
testdata/many_conditionals.emb folding into case 0 of the
LargeConditionals switch.
This was referenced May 20, 2026
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.
Three composing changes to the optimized
Ok()switch generator:Case-label sort. Each switch arm's labels are sorted by underlying integer/enum value before emit. Sorted cases give older embedded GCCs (the ones in
microblaze-elfand many bare-metalarm-none-eabitoolchains) a better shot at emitting a dense jump table rather than an if-ladder.Identical-body coalescing. Cases whose rendered body text is identical are merged into a single arm with multiple
case X:labels. The C++ compiler emits one body for the whole arm — a real text-size win once the next PR (disjunction matching) starts producing such pairs.Multi-field per case. When two conditional fields share a discriminant + case value (
if tag == 0: aandif tag == 0: b), they bundle into the same case arm rather than the second falling back to a separate if-statement.The
ok_method_switch_casetemplate becomesok_method_switch_armtaking pre-formatted${case_labels}and${case_body}strings. Single-label single-field arms render identically, so golden churn is limited to thef0_copyfield intestdata/many_conditionals.embfolding into case 0 of theLargeConditionalsswitch.Size impact (cumulative vs. master)
Stacked on #253.