Skip to content

Conversation

@AaronWebster
Copy link
Collaborator

This change significantly optimizes the generated C++ code for
structures utilizing conditional fields.

Key optimizations include:

  • Two-Pass Generation: Implemented a strategy to identify and cache
    frequently used subexpressions (e.g., tag reads) within Ok(),
    Equals(), and UncheckedEquals() methods.
    • This involves a first pass to count subexpression usage, followed
      by a second pass that caches repeated expressions in local
      variables while inlining unique ones.
  • Reduced Redundancy: Minimizes redundant computations, leading to CPU
    performance improvements and reduced stack usage for unique
    conditions.

Performance Impact (on testdata/many_conditionals.emb with 10,000
iterations x 100 tags):

  • Runtime Speedup: ~8.7x faster (from ~1.83s to ~0.21s).
  • Binary Size Reduction: ~47% smaller (from 57848 bytes to 30472
    bytes for many_conditionals_benchmark with -c opt).

Additional updates:

  • Test infrastructure was enhanced to support passing compiler flags to
    golden tests, resolving an issue with no_enum_traits_golden_test.
  • New test data (testdata/many_conditionals.emb) and a benchmark
    (compiler/back_end/cpp/testcode/many_conditionals_benchmark.cc)
    were added to verify performance.

AaronWebster and others added 9 commits November 25, 2025 12:55
Optimized the generated C++ code for structures with conditional fields
by identifying and caching repeated subexpressions (e.g., tag reads)
in the `Ok()`, `Equals()`, and `UncheckedEquals()` methods.

This approach uses a two-pass generation strategy:
1.  Count usage of existence condition subexpressions.
2.  Generate code where frequently used subexpressions are cached in
    local variables at the method scope, while unique expressions are
    inlined.

This reduces redundant computations (CPU optimization) without
introducing excessive stack usage for unique conditions (Memory
optimization).

Also updated test infrastructure to support passing compiler flags to
golden tests, fixing `no_enum_traits_golden_test`.

Added `testdata/many_conditionals.emb` and `compiler/back_end/cpp/testcode/many_conditionals_benchmark.cc`
to verify performance of conditional fields.

Performance Analysis (10,000 iterations x 100 tags on `testdata/many_conditionals.emb`):
- Baseline Runtime: ~1.83s
- Optimized Runtime: ~0.21s (Speedup: ~8.7x)

Binary Size Impact (many_conditionals_benchmark, -c opt):
- Baseline: 57848 bytes (text: 56112)
- Optimized: 30472 bytes (text: 28736) -> ~47% reduction
Copilot AI and others added 3 commits November 26, 2025 00:16
…nd-tests

Fix C++ backend conditional optimization: formatting and golden file regeneration
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.

1 participant