gc: Recognize sysimage objects by address in the mark loop #64061
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)
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.
@vtjnash Any other thoughts?
What is this change for? It should probably have an accompanying test
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.
Any other performance numbers to track, such as how this does on common hardware?
@nanosoldier runbenchmarks()
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.
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.
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.
This is a simplified version of what was approved before, so going ahead and merging this.
vtjnash
maleadt
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