Skip to content

Statically-known existence conditions: drop or simplify the check#259

Open
AaronWebster wants to merge 1 commit into
emboss/tail-form-returnfrom
emboss/static-conditions
Open

Statically-known existence conditions: drop or simplify the check#259
AaronWebster wants to merge 1 commit into
emboss/tail-form-returnfrom
emboss/static-conditions

Conversation

@AaronWebster
Copy link
Copy Markdown
Collaborator

Many Emboss schemas have fields whose existence_condition the front end has already constant-folded to a boolean literal:

  • if false: fields are never present, so Ok() needs no check for them at all. PR Optimize C++ generated code for structures with many conditional fields #241 still emitted the full has_X() guard + Ok() check; both branches are dead.
  • Unconditional fields (and if true: fields) carry a literal true existence. has_X() is always Known and always true, so the standard ok_method_test pattern collapses from two lines to one bare if (!x().Ok()) return false; check.

A fast pre-pass in _generate_optimized_ok_method_body recognizes both shapes and routes them to the short emit paths. if false: fields are skipped; the always-true case emits the bare Ok() check. All unconditional fields share one group so they emit contiguously at the position of the first such field — preserves PR #241's field-batching shape so the compiler's view of the function body stays tight.

The docstring on _generate_optimized_ok_method_body is rewritten to describe the four emit paths the function now routes fields through (skipped / unconditional / switch arm / ok_method_test).

Size impact (cumulative vs. master)

Target Metric Master PR Delta
ARM Thumb-2 TU .text 18962 17974 −988 (−5.2%)
ARM Thumb-2 LargeConditionals::Ok() 5382 4930 −452 (−8.4%)
ARM Thumb-2 DisjunctionConditionals::Ok() 358 262 −96 (−26.8%)
MicroBlaze TU .text 43640 35796 −7844 (−18.0%)
MicroBlaze LargeConditionals::Ok() 14824 8308 −6516 (−44.0%)
MicroBlaze DisjunctionConditionals::Ok() 640 532 −108 (−16.9%)
Host x86-64 TU .text 29166 28242 −924 (−3.2%)
Host x86-64 LargeConditionals::Ok() 3948 3040 −908 (−23.0%)
Host x86-64 DisjunctionConditionals::Ok() 431 354 −77 (−17.9%)

Incremental delta vs. #258: 0 across the board. The C++ compiler optimizes both source patterns identically at -Os, so this PR is a source-quality improvement (32 golden headers shrink by hundreds of lines apiece, generated code is easier to read) with no runtime machine-code cost.

Stacked on #258.

Many Emboss schemas have fields whose existence_condition the front
end has already constant-folded to a boolean literal:

  * \`if false:\` fields are never present, so Ok() needs no check
    for them at all. PR 241 still emitted the full has_X() guard +
    Ok() check; both branches are dead.
  * Unconditional fields (and \`if true:\` fields) carry a literal
    \`true\` existence. has_X() is always Known and always true, so
    the standard ok_method_test pattern

      if (!has_x().Known()) return false;
      if (has_x().ValueOrDefault() && !x().Ok()) return false;

    collapses to a bare

      if (!x().Ok()) return false;

A fast pre-pass in _generate_optimized_ok_method_body recognizes
both shapes (existence_condition.type.boolean has a literal value)
and routes them to the short emit paths. \`if false:\` fields are
skipped; the always-true case emits the bare Ok() check.

Wide golden churn: most structs have several unconditional fields
(virtual size fields like IntrinsicSizeInBytes/Max/Min plus any
non-conditional payload), so most Ok() methods shrink by several
lines apiece.

The docstring on _generate_optimized_ok_method_body is rewritten to
describe the four emit paths the function now routes fields through
(skipped / unconditional / switch arm / ok_method_test) — the
original PR 241 docstring referenced only two paths and a duplicate-
case fallthrough that no longer exists.
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.

2 participants