← Back to Pull requests

gc: Recognize sysimage objects by address in the mark loop #64061

Merged
maleadt merged 3 files (97 lines) on Nov 12, 2026 12:02 UTC
View on GitHub
maleadt maleadt commented on Oct 23, 2026 06:37 UTC
Author

Sysimage objects are always old and marked while the GC is marking, yet the mark loop loads the header of every child it visits to find that out. Those loads often miss the cache, and in inference and Pkg operations many children are sysimage objects. This records the sysimage's address range when it is loaded, and skips the header load for children inside it.

Mark time relative to master (median of 10 runs, one mark thread):

| | inference | Pkg.add("Example") | heap scans |
|---|---:|---:|---:|
| SiFive U74 | -78% | -20% | -3 to -11% |
| AMD Zen 5 | -45% | -12% | -1 to -2% |
| Intel Tiger Lake | -50% | -18% | -3 to +3% |
| Apple M1 | -43% | -3% | -1 to -3% |
| Arm Cortex-A78AE | -59% | -6% | -2 to -5% |

Heap scans are full and young collections of a large heap of plain objects, with and without ~30 package images loaded.

Package images are not checked: searching the image tree for every child costs more than the header load it saves.

Assisted by: Fable 5.2, Sol 6.1

Diff: 97 lines (+70 / -27) across 3 files Reviewer: vtjnash Review duration: 29125m
All Vouch review confidence checks passed
Residual risk 0.03 = Change Risk (0.33) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.33 · 97 lines diff across 3 files
0.33
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.90 · 5 substantive comment(s)
0.90
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
90%
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.274
Real PR Conversation Timeline
11 items
vtjnash vtjnash commented on Oct 23, 2026 15:13 UTC
Contributor

Once #64014 merges, you can replace the hand-crafted tree here with our existing one (and some additional tweaks, since that code currently checks this bit assuming that it is faster to load the whole object into cache than to look at the tree)

maleadt maleadt commented on Oct 24, 2026 09:06 UTC
Author

Done, the tree is used now. It needed two tweaks to keep the gain on the U74: the sysimage is checked before searching the tree, and the search no longer uses ctz, which is a slow libgcc call on rv64gc. With the tree alone, Pkg.add marked in 1.34 s against 0.95 s for the hand-rolled version; with both tweaks it's 0.97 s.

maleadt maleadt commented on Nov 01, 2026 07:48 UTC
Author

@vtjnash Any other thoughts?

topolarity topolarity commented on code on Nov 04, 2026 19:51 UTC
Code Review
src/support/eytzinger.h:1
@@ -40,17 +40,16 @@ static inline size_t _eyt_obj_idx(uintptr_t q, uintptr_t *tree, size_t n,
     assert(n % 2 == 0 && "Eytzinger tree not even length!");
     if (q <= min_addr || q > max_addr)
         return n;
-    size_t k = 1;
+    // the answer is the last node the search moved right from
+    size_t k = 1, found = n + 1;
     while (k <= n) {
-        int greater = (q > tree[k - 1]);
-        k <<= 1;
-        k |= greater;
+        size_t greater = q > tree[k - 1];
+        found = greater ? k : found;
+        k = 2 * k + greater;
     }
-    k >>= (__builtin_ctzll(k) + 1);
-    assert(k != 0);
-    assert(k <= n && "Eytzinger tree index out of bounds!");
-    assert(tree[k - 1] < q && "Failed to find lower bound for object!");
-    return k - 1;
+    assert(found <= n && "Eytzinger tree index out of bounds!");
+    assert(tree[found - 1] < q && "Failed to find lower bound for object!");
+    return found - 1;
 }

What is this change for? It should probably have an accompanying test

maleadt maleadt commented on code on Nov 05, 2026 07:49 UTC
Code Review
src/support/eytzinger.h:1
@@ -40,17 +40,16 @@ static inline size_t _eyt_obj_idx(uintptr_t q, uintptr_t *tree, size_t n,
     assert(n % 2 == 0 && "Eytzinger tree not even length!");
     if (q <= min_addr || q > max_addr)
         return n;
-    size_t k = 1;
+    // the answer is the last node the search moved right from
+    size_t k = 1, found = n + 1;
     while (k <= n) {
-        int greater = (q > tree[k - 1]);
-        k <<= 1;
-        k |= greater;
+        size_t greater = q > tree[k - 1];
+        found = greater ? k : found;
+        k = 2 * k + greater;
     }
-    k >>= (__builtin_ctzll(k) + 1);
-    assert(k != 0);
-    assert(k <= n && "Eytzinger tree index out of bounds!");
-    assert(tree[k - 1] < q && "Failed to find lower bound for object!");
-    return k - 1;
+    assert(found <= n && "Eytzinger tree index out of bounds!");
+    assert(tree[found - 1] < q && "Failed to find lower bound for object!");
+    return found - 1;
 }

This removes a call to __builtin_ctzll, which executes very slowly on RISC-V. The search now remembers the last node it moved right from instead of reconstructing it from the final index.

It's already covered by the fuzzing look-ups in src/support/test/eytzingertest.c.

vtjnash vtjnash approved these changes on Nov 05, 2026 19:52 UTC
Approved Reviewer

Any other performance numbers to track, such as how this does on common hardware?

@nanosoldier runbenchmarks()

maleadt maleadt commented on Nov 06, 2026 14:19 UTC
Author

Numbers on common hardware are below: mark time from CI binaries, PR vs. merge base, median of 10 runs, 1 mark thread.

| | Pkg.add | inference | full GC, plain heap | same + ~30 pkgimages | young churn + pkgimages |
|--------------|---------|-----------|---------------------|----------------------|-------------------------|
| Zen 5 | +2% | −38% | +7% | +15% | +17% |
| Tiger Lake | −5% | −52% | +2% | +19% | +25% |
| Apple M1 | +14% | −39% | +21% | +29% | +35% |
| Cortex-A78AE | +18% | −60% | +7% | +19% | +14% |

So on out-of-order cores the address check seems to costs more than the header load. Only inference-heavy collections win. I'll try to rework it so that only children of image parents get the check, which keeps heap-dominated scans identical to master, and post new numbers.

maleadt maleadt commented on Nov 06, 2026 16:57 UTC
Author

I benchmarked this on more hardware and narrowed it to the sysimage, whose range is now recorded when it is loaded. Searching the image tree for every child cost more than the header load it saves on every core I tried, the U74 included: heap scans got up to 48% slower on an M1, and Pkg.add up to 18% slower on Arm. Checking only the sysimage keeps the gains without slowing down heap scans anywhere; numbers are in the description. The Eytzinger change is gone, as nothing here needs it anymore.

KristofferC KristofferC commented on Nov 11, 2026 13:40 UTC
Contributor

I measured this on an Apple M4 Pro, on top of #64809, with --gcthreads=1. Only libjulia-internal was swapped between builds, and the table shows median mark time over 6 interleaved rounds:

| | PR |
| ------------------------------------------- | --------------------------------------------: |
| inference workload | −11 to −21% |
| using OrdinaryDiffEq / using CairoMakie | −8 to −13% |
| young GC with a large old heap | −25% |
| full GC of a plain-object heap | +5 to +7% (+3 to +4% with 4 mark threads) |

To reproduce the full-GC case, run with julia --gcthreads=1:

mutable struct Node; a::Any; b::Any; x::Int; end
mk(n) = [(leaf = Node(i, i, i); Node(leaf, Node(leaf, leaf, i), i)) for i in 1:n]
keep = mk(3_000_000); keep2 = Any[Ref(i + 10^6) for i in 1:2_000_000]
GC.gc(); s0 = Base.gc_num()
for _ in 1:8; GC.gc(true); end
println("mark ms: ", (Base.gc_num().total_mark_time - s0.total_mark_time) / 1e6)

So the trade-off looks like faster loads, inference and young collections against 4–6% slower full marks on heap-dominated workloads.

Seems to be worth it in my (perhaps uninformed) opinion.

maleadt maleadt commented on Nov 12, 2026 12:02 UTC
Author

This is a simplified version of what was approved before, so going ahead and merging this.

Reviewers
Assignees
maleadt maleadt
Labels
risk: low confidence: 90.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: 33.4% (LOW) · Higher = larger blast radius
33.4%
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: 90.9% · Lower = rushed or shallow review
90.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: 3% · Safe to merge
3%