Skip to content

perf(core): always inline rule_matches into the generic evaluator (#72; #73 analysed) - #102

Merged
akosidencio merged 3 commits into
devfrom
perf/v0.2.1-core
Nov 30, 2026
Merged

akosidencio merged 3 commits into
devfrom
perf/v0.2.1-core

Conversation

@akosidencio

Copy link
Copy Markdown
Member

Core performance for 0.2.1: this fixes the root cause of #72. It does not recover #73, and neither issue's ≤ 1.010 gate is met until the dedicated Linux host confirms it.

Root cause of #72

I disassembled the release A/B harness (scripts/release-benchmarks/lib/core_ab.rs, built against each release):

Build evaluate_variant_at_depth rule_matches
v0.1.31 1,800 instructions, rule_matches inlined no symbol
v0.2.0 694 instructions separate function of 893 instructions, called once per rule
  • In 0.2.0 the evaluator became generic over its trace recorder and is instantiated twice: NoTrace for evaluate_variant and Recorder for explain(). rule_matches gained a second caller, so LLVM stopped inlining it.
  • A plain #[inline] hint does not change this (verified: identical code). #[inline(always)] restores the 0.1.31 shape of the rule loop.
  • That fits the measured cost, which grows with rule count: +2.2 ns at 1 rule and +10.4 ns at 10 rules on the Linux host.

#73 (weighted multivariate): root cause analysis, not fixed here

  • The weighted path also changed in 0.2.0. The picker returns an index, and the evaluator then does a second bounds-checked variants.get(index).
  • Removing that lookup recovers weighted multivariate locally, by 3.5–4.4 % against v0.2.0, real at 95 % CI. I tried three forms:
    • return (index, &variant), inlined;
    • return &variant with the index computed lazily, only for explain();
    • the same, kept out of line.
  • Each form slowed prerequisite_chain_5 by 5–12 % (CI excludes 1). Forms 1–2 enlarge the recursive evaluator; form 3 adds a call to every multivariate evaluation, and also slowed boolean.
  • With function and block alignment forced (-align-all-functions=6 -align-all-nofallthru-blocks=5, experiment builds only), the chain cost stayed (×1.036 [1.004, 1.046]). So it is not only a layout artifact.
  • I did not ship a change that trades one regression for another. Options for the maintainer are in the release report.

Local A/B (macOS laptop; same harness, B→C→C→B rounds, ratio of medians with a 95 % bootstrap CI; indicative only):

Case v0.2.0 vs v0.1.31 this PR vs v0.2.0
targeting_10_rules 1.086 [1.053, 1.130] 0.933 [0.903, 0.950]
targeting_1_rule 1.032 [1.022, 1.059] 0.991 [0.957, 1.025]
weighted_multivariate 1.032 [1.008, 1.057] 1.015 [0.952, 1.059]
prerequisite_chain_5 0.987 [0.968, 1.003] 1.019 [0.995, 1.050]
boolean, rollout, lookup, all_flags flat CI includes 1

With alignment forced: targeting_10_rules 0.917 [0.888, 0.962] vs v0.2.0, and prerequisite_chain_5 0.987.

Gates (#72 and #73: CI upper bound ≤ 1.010 against v0.1.31 on the dedicated host, and no other benchmark more than 1 % slower): not met yet. They need scripts/release-benchmarks/v0.2.0-linux.sh --baseline v0.1.31 --candidate <this PR's head> on the dedicated Linux host, which I don't have. This laptop's run-to-run noise is several percent.

Correctness: no semantic change. Core unit tests, bucketing tests and the shared evaluation vectors (explain() included) pass.

Refs #72, #73

Root cause of the 0.2.0 targeting slowdown, from disassembly of the release
A/B harness: the evaluator became generic over its trace recorder and is
instantiated twice (evaluate_variant and explain). rule_matches then had
two callers and LLVM stopped inlining it (a plain #[inline] hint does not
change that), so every rule of every evaluation paid a call. 0.1.31: one
1,800-instruction evaluator, rule_matches inlined. 0.2.0: evaluator 694
instructions plus an out-of-line rule_matches of 893, one call per rule.

#[inline(always)] restores the 0.1.31 shape for the rule loop. No semantic
change; core tests, bucketing tests and the shared evaluation vectors
(explain included) pass unchanged.

Local A/B (macOS, release_core_ab, B-C-C-B): targeting_10_rules vs 0.2.0
0.933 [0.903, 0.950]; other cases inconclusive at 1 %. The release gate
needs the dedicated Linux host.
@akosidencio
akosidencio merged commit cf6547c into dev Nov 30, 2026
43 checks passed
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