Skip to content

Sort switch cases by value; coalesce identical-body cases#254

Merged
AaronWebster merged 1 commit into
emboss/render-dedupfrom
emboss/sort-coalesce
Jun 4, 2026
Merged

Sort switch cases by value; coalesce identical-body cases#254
AaronWebster merged 1 commit into
emboss/render-dedupfrom
emboss/sort-coalesce

Conversation

@AaronWebster

Copy link
Copy Markdown
Collaborator

Three composing changes to the optimized Ok() switch generator:

  1. 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-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 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.

  3. Multi-field per case. When two conditional fields share a discriminant + case value (if tag == 0: a and if 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_case template becomes ok_method_switch_arm taking pre-formatted ${case_labels} and ${case_body} strings. Single-label single-field arms render identically, so golden churn is limited to the f0_copy field in testdata/many_conditionals.emb folding into case 0 of the LargeConditionals switch.

Size impact (cumulative vs. master)

Target Metric Master PR Delta
ARM Thumb-2 TU .text 18962 18296 −666 (−3.5%)
ARM Thumb-2 LargeConditionals::Ok() 5382 4746 −636 (−11.8%)
MicroBlaze TU .text 43640 42700 −940 (−2.2%)
MicroBlaze LargeConditionals::Ok() 14824 14104 −720 (−4.9%)
Host x86-64 TU .text 29166 28344 −822 (−2.8%)
Host x86-64 LargeConditionals::Ok() 3948 3065 −883 (−22.4%)

Stacked on #253.

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.
@AaronWebster AaronWebster force-pushed the emboss/render-dedup branch from caa728b to 507136e Compare June 3, 2026 23:04
@AaronWebster AaronWebster force-pushed the emboss/sort-coalesce branch from 75dce5d to 409a587 Compare June 3, 2026 23:04
@AaronWebster AaronWebster merged commit a5f26ac into emboss/render-dedup Jun 4, 2026
7 checks passed
@AaronWebster AaronWebster deleted the emboss/sort-coalesce branch June 4, 2026 00:30
AaronWebster added a commit that referenced this pull request Jun 4, 2026
* Render switch discriminant once per group; drop dead inner scope

The optimized Ok() switch-block code in _generate_optimized_ok_method_body
was rendering each switch group's discriminant twice: once unscoped at
grouping time to build the SWITCH: key, and again with the active
ExpressionScope at emit time. Render it once with the scope and reuse
the result. The result is stable for equivalent discriminants because
ExpressionScope.add dedupes by inner rendered form, so it still works
as a grouping key.

The ok_method_switch_block template's \${inner_scope_definitions}
placeholder was unused — the inner ExpressionScope it referenced had
nothing added to it. Removed both, which also drops the blank line
each switch block carried in the goldens.

No behavioral change; purely compile-time cleanup. Golden churn
limited to the blank-line removal in four files (one line per existing
switch block).

* Sort switch cases by value; coalesce identical-body cases (#254)

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.
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.

3 participants