← Back to Pull requests

types: memoize structural facts about a UnionAll in a flag word (rebased #63778) #63932

Open
KristofferC opened this pull request with 859 lines diff across 9 files
View on GitHub
KristofferC KristofferC commented on Oct 18, 2026 18:48 UTC
Author

Rebased version of #63778 by @Keno onto current master (the original conflicts and isn't maintainer-editable).

From the original description:

This backports the flags word from #63530. According to Claude this is -27% load time on using ReverseDiff, although as repeatedly stated elsewhere the real bug is doing so much subtyping work at load time in the first place.

Conflict resolution notes:

  • jltypes.c: master added DataType fast paths to has_free_typevars / jl_has_bound_typevars; those are subsumed by the visitors here (free_typevar_visit / bound_typevar_visit already return early on closed datatypes).

  • subtype.c: master (#63668) made body_occurs_inv lazy in subtype_unionall via vb_body_occurs_inv. This keeps it lazy but computes it through the new UnionAll flag memo (unionall_body_occurs_inv), so the varbinding now stores the UnionAll (vb.ua) instead of its body.
  • 🤖 Generated with Claude Code

    Diff: 859 lines (+459 / -400) across 9 files Reviewer: KristofferC Review duration: 17225m
    All Vouch review confidence checks passed
    Residual risk 0.11 = Change Risk (0.74) × (1 − Review Confidence (0.85)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.74 · 859 lines diff across 9 files
    0.74
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.94 · 4 substantive comment(s)
    0.94
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    84%
    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.400
    Self Review self_review
    0.140
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    5 items
    KristofferC KristofferC commented on code on Oct 18, 2026 18:51 UTC
    Code Review
    src/julia.h:1
    @@ -638,7 +638,20 @@ typedef struct JL_GC_TRACKED_TYPE {
         JL_DATA_TYPE
         jl_tvar_t *JL_NONNULL var;
         jl_value_t *JL_NONNULL body;
    +// hidden fields (not part of the Julia-visible layout, so a UnionAll must be

    From @vtjnash on the original PR (https://github.com/JuliaLang/julia/pull/63778#discussion_r4272730920):

    Why do we need to hide this? Hidden fields are annoying, and it seems reasonable to make this visible (unless you think it'll get deleted again with other work in changing representations?)

    Keno Keno commented on code on Oct 18, 2026 19:03 UTC
    Code Review
    src/julia.h:1
    @@ -638,7 +638,20 @@ typedef struct JL_GC_TRACKED_TYPE {
         JL_DATA_TYPE
         jl_tvar_t *JL_NONNULL var;
         jl_value_t *JL_NONNULL body;
    +// hidden fields (not part of the Julia-visible layout, so a UnionAll must be

    Sorry, I was traveling. We need to hide it because it can change in ways that aren't modeled by our object model. The fields can be set upon access by the runtime and reset by swap-in of an egal pointer. If this is runtime visible, it will exacerbate the getfield egality problem that the unionall refactor is trying to remedy. Possibly in the future we can add the notion of a memorized field, assuming the result of the computation is stable under egality (which I think it should be, but didn't 100% verify), but for now, I think this is best.

    KristofferC KristofferC commented on code on Oct 22, 2026 19:58 UTC
    Code Review
    src/julia.h:1
    @@ -638,7 +638,20 @@ typedef struct JL_GC_TRACKED_TYPE {
         JL_DATA_TYPE
         jl_tvar_t *JL_NONNULL var;
         jl_value_t *JL_NONNULL body;
    +// hidden fields (not part of the Julia-visible layout, so a UnionAll must be

    @vtjnash does that sound reasonable to you?

    IanButterworth IanButterworth commented on Oct 24, 2026 13:42 UTC
    Contributor

    Claude's thoughts:

    ---

    Review notes:

    1. Generic constructors still make UnionAlls without room for the flag word. The layout size of UnionAll is still 16 bytes, so jl_new_structv, jl_new_struct_uninit, jl_new_bits (via unsafe_load(::Ptr{UnionAll})) and codegen's :new all produce a node that ends where flags begins. Core.eval(Expr(:new, UnionAll, TypeVar(:T), Any)) works today, and the old test in test/subtype.jl built nodes this way. The first type query then reads and fetch_ors 4 bytes past the object. Today those bytes are padding in the 32-byte GC slot, so nothing else is overwritten. But they are not zeroed: I saw one fresh node in 1000 that already had a nonzero flag word, so a stale "known" bit could give a wrong has_free_typevars or occurs-invariant answer. Two possible fixes: reject UnionAll in the generic constructors and in emit_new_struct, or make the layout size sizeof(jl_unionall_t) and zero the extra bytes. Method has the same shape, but its hidden fields are only read on methods the runtime made itself.
    2. typeeq_vars_bound_in_env now depends on whether the memo is set. It skips a sub-UnionAll that is known to be closed. A cold walk descends into it and asks lookup_binding for the inner binder. Unlike the other walkers it ignores binders, so if that binder is currently in e->vars, the cold walk returns 1 and the memoized walk returns 0. Probably harmless, but it makes typeeq_is_dangling_key depend on query history.
    3. Minor, in commit 1: the "behavior is unchanged except TypeApp" claim also misses that Intersect nodes are now entered by may_substitute_ub, typeeq_vars_bound_in_env and type_references_deferred. All three changes are in the conservative direction.

    Reviewers
    Assignees
    KristofferC KristofferC
    Labels
    risk: low confidence: 84.9% 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: 73.7% (HIGH) · Higher = larger blast radius
    73.7%
    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: 84.9% · Lower = rushed or shallow review
    84.9%
    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: 11.1% · Safe to merge
    11.1%