← Back to Pull requests

inference: Key const-prop cache entries by their constant arguments #64707

Open
IanButterworth opened this pull request with 81 lines diff across 4 files
View on GitHub
IanButterworth IanButterworth commented on Nov 06, 2026 13:08 UTC
Author

Claude:

---

Inference time is quadratic in the number of distinct constants a MethodInstance is const-propagated with. This caused the coverage hang in #64701, where 100 sin calls create about 65,000 cache entries on about 110 MethodInstances (7,829 on -(::Float64, ::Float64) alone).

  • The cost: each const-prop lookup compared against every entry for that MethodInstance, about 45% of samples at 100 calls. Lookups for non-constant results also walked past those entries.

  • Now: constant entries are kept apart from non-constant ones and found by a key from the MethodInstance and its constant arguments. The full comparison still runs on each candidate.

  • Bool is left out of the key: a conditional can be lattice-equal to a Bool constant, so keying on it would miss matches, including cached rejections that stop a const-prop being retried.
  • code_typed of a function with N constant sin calls, on a coverage build of ad7c34a1c3 (macOS aarch64, --code-coverage=all, single runs):

    | N | master | this PR |
    |---|---|---|
    | 100 | 29.18 s | 7.30 s |
    | 200 | over 150 s (on 996bec08) | 14.16 s |
    | 1000 | not measured | 93.74 s |

    Compiler/inline with its 1000 sin calls goes from still stuck after 20 minutes (on 996bec08) to 1m55s, with the same pass and fail counts as #64703 at 10 calls. That test change is independent of this one and can merge first.

    Checks

  • The Compiler tests pass on a regular build (macOS aarch64).

  • On a small regular-build workload, all 2,375 real const-prop queries returned the same result as a reimplementation of the old scan, including an always-true conditional against a stored true. This is a sample, and an earlier version of the key also passed it apart from that added case.

  • In that workload the largest bucket is 3 entries, down from 63 with isbits constants only. The second commit makes that change and can be dropped if hashing non-isbits values costs too much.
  • Regular builds (inference and compiler categories of julia-ci-benchmarks, against ad7c34a1c3, 3 blocks, Apple M5 Pro): no regressions, and a test-only control build flags nothing.

  • infer_many_constprop_entries (added in JuliaCI/julia-ci-benchmarks#1): -63.3% instructions

  • infer_many_method_matches: -4.2%

  • infer_many_opaque_closures: -2.9%
  • TTFX on this PR found no robust regressions or improvements (39 tasks, 2 blocks).

    The remaining cost is linear, but each sin still inlines about 5,300 statements under coverage. External packages that read the index field or construct InferenceCache positionally haven't been checked.

    Reviewed with Codex (gpt-6-astra), which found the Bool problem, and Fable 5.1, which suggested the non-isbits keys.

    Written with Claude Code (Opus 5.5).

    Diff: 81 lines (+69 / -12) across 4 files Reviewer: KristofferC Review duration: 42060m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.39) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.39 · 81 lines diff across 4 files
    0.39
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.91 · 3 substantive comment(s)
    0.91
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    91%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.264
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    4 items
    KristofferC KristofferC commented on code on Nov 06, 2026 15:32 UTC
    Code Review
    Compiler/test/inference.jl:1
    @@ -355,6 +355,52 @@ end
                 lattice, mi, argtypes, cache, UInt(2)))
             @test Compiler.constprop_cache_lookup(
                 lattice, mi, argtypes, cache, UInt(3)) === nothing
    +
    +        cache = Compiler.InferenceCache()

    It is kind of hard to understand what property these tests actually test. If the effect of this is performance, we could maybe add an entry to https://github.com/JuliaCI/julia-ci-benchmarks`

    IanButterworth IanButterworth commented on code on Nov 06, 2026 16:04 UTC
    Code Review
    Compiler/test/inference.jl:1
    @@ -355,6 +355,52 @@ end
                 lattice, mi, argtypes, cache, UInt(2)))
             @test Compiler.constprop_cache_lookup(
                 lattice, mi, argtypes, cache, UInt(3)) === nothing
    +
    +        cache = Compiler.InferenceCache()

    yeah. https://github.com/JuliaCI/julia-ci-benchmarks/pull/1

    I'll let the TTFX job finish here then remove the test

    IanButterworth IanButterworth commented on code on Nov 06, 2026 17:19 UTC
    Code Review
    Compiler/test/inference.jl:1
    @@ -355,6 +355,52 @@ end
                 lattice, mi, argtypes, cache, UInt(2)))
             @test Compiler.constprop_cache_lookup(
                 lattice, mi, argtypes, cache, UInt(3)) === nothing
    +
    +        cache = Compiler.InferenceCache()

    TTFX no change. I scaled the test back.

    Reviewers
    Assignees
    IanButterworth IanButterworth
    Labels
    risk: low confidence: 91.4% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 38.8% (MEDIUM) · Higher = larger blast radius
    38.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 91.4% · Lower = rushed or shallow review
    91.4%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 3.3% · Safe to merge
    3.3%