KotobaDocs

10 Making generated code faster

Optimization performs the same computation at lower cost. In Amu, correctness includes fuel, traps, effects, and arena observations as well as result values. Removing checks to improve timing is not sufficient grounds for adoption.

Locate the cost

Repeated expressions, calls, register/stack movement, branches, buffer accesses, dynamic type work, bounds checks, and fuel updates can all cost time. What can be reduced depends on known values, types, aliases, and effects. The constant 40 + 2 in chapter 8 is the smallest example of knowing a result before execution.

Techniques to look for

TechniqueHow it reduces workConditions to preserve
Propagate constants and value factsAvoid repeated computation and branching; use immediates and suitable instructionsi64 wrap behavior, types, exceptions and traps
Inline small functions and use tail callsReduce calls, frames, and movesArgument order, live values, ABI, fuel
Recognize structural native kernelsEmit suitable instruction sequences for recognized computationsStructural guards rather than benchmark names
Unroll loops and use SIMDReduce branch overhead or process multiple elements per instructionAll elements, tails, bounds, CPU support
Build tables from constantsReplace repeated decision-tree traversal with lookupValues fully determined by the original function
Hoist or batch checksEstablish conditions earlier and reduce redundant decisionsMatching insufficient-fuel and invalid-index behavior

41-a64gen contains many recognition and emission paths. Read their entry predicates and fallback paths rather than assuming that every listed technique applies to all inputs. Historical candidates are not necessarily the adopted source.

Turn a decision tree into a table

The scalar threshold-tree experiment recognizes a pure tree that branches on constant thresholds and ends in integer constant leaves. After checking the complete admitted shape, it replaces traversal with a 64-entry table and endpoint handling. Unknown effectful calls and dynamic leaves do not qualify. The table is derived from the original function's information, not selected answers keyed by benchmark name. Read the experiment record for its verification and measurement scope.

Preserve fuel and bounds checks

Known iteration counts or index ranges can allow repeated checks to be combined. If the budget expires partway through, execution must still stop at the corresponding point with the appropriate changes already made. A sufficient-fuel fast path plus an original-order path also needs that argument. The sufficient-fuel region experiment passed correctness comparisons but failed its performance criteria and rejected global promotion. Correctness and speed are separate observations.

Fuel semantics are profile-specific. Do not equate ordinary native settings with a metered receipt profile. Reducing CPU instructions does not automatically permit reducing semantic charges.

Read the public speed figures

The 15 October 2026 Apple M4 observation covered 19 translated Embench ports with one compiler snapshot and one accepted sample per workload. Its geometric mean execution time was about 0.80× C time. The number includes audited algorithm substitutions and a SHA-instruction difference. It is not a released-toolchain result, certified score, or universal speed ranking. Read the public article and audit with the ratio.

The separate same-algorithm M104 baseline was about 0.8213×; it is a different measurement. Neither historical result is a new measurement of today's loaderless native compiler. The compiler lineage records the distinction.

Exercise and answer

If output is unchanged and code is 10% smaller, is it 10% faster? Not necessarily. Dependencies, caches, branches, and timing variability affect execution. Check non-value semantics too, then measure under matching workload, host, and compiler conditions.

Previous · Contents · Next: build performance