Tail-form return in terminal switches#258
Open
AaronWebster wants to merge 1 commit into
Open
Conversation
When the optimized Ok() switch is the function's tail — last group
emitted, no [requires] clause after — arms that validate exactly one
field with no residual collapse from
case K:
if (!field().Ok()) return false;
break;
to
case K:
return field().Ok();
Saves one conditional branch per qualifying arm. On Thumb-2 /
MicroBlaze (where -Os is conservative about merging cmp+beq
sequences) and on older compilers that don't see through the
if/return-false/break shape, this trims a few bytes per case across
large tagged-union schemas.
Per-arm decision: any arm that doesn't qualify (multi-field,
residual, or the switch isn't terminal) keeps the break-form body.
Mixing forms within one switch is safe — the default fallthrough
lands on the function's \`return true;\` either way.
Golden churn: many_conditionals.emb.h shrinks substantially (the 99
single-field cases all collapse to tail-form); condition.emb.h and
parameters.emb.h see smaller similar collapses.
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.
When the optimized
Ok()switch is the function's tail — last group emitted, no[requires]clause after — arms that validate exactly one field with no residual collapse fromto
Saves one conditional branch per qualifying arm. On Thumb-2 / MicroBlaze (where
-Osis conservative about merging cmp+beq sequences) and on older compilers that don't see through the if/return-false/break shape, this trims a few bytes per case across large tagged-union schemas.Per-arm decision: any arm that doesn't qualify (multi-field, residual, or the switch isn't terminal) keeps the break-form body. Mixing forms within one switch is safe — the default fallthrough lands on the function's
return true;either way.Golden churn:
many_conditionals.emb.hshrinks substantially (the 99 single-field cases all collapse to tail-form);condition.emb.handparameters.emb.hsee smaller similar collapses.Size impact (cumulative vs. master)
Note:
LargeConditionals::Ok()measured bynmgrows slightly on Thumb-2 and MicroBlaze for this PR alone (+302 / +604 bytes), but TU.textstill shrinks. The compiler is inlining tail-form arms into callers more aggressively, so reduced overall code lives inOk()'s callers rather thanOk()itself.Stacked on #257.