Feature #22707
openTrigger nofree majors per heap instead of simple GC counting
Description
Patches¶
Background¶
Currently when a heap decides whether to trigger a major GC by nofree (ie. when that heap has run out of slots for allocation), we were just checking how many GC's had been run across the whole objspace since the last major. If that number was greater than RVALUE_OLD_AGE we request a major GC, otherwise we just grow the heap.
This method doesn't take into account the state that each individual heap within that objspace could be in and whether the heap currently being swept actually needed a major or not.
For example, a heap that allocates a burst of young objects and becomes short of free slots, could trigger a major GC that frees almost nothing, just because other heaps in the objspace have already advanced the counter.
Proposed changes¶
The linked PR changes the heuristics used to trigger a major by nofree. We track a new per heap counter deficits_since_major. This is the number of minor sweeps since the last major that have left that heap short of min_free_slots. When a heap finishes a sweep it will signal that it requires a major by nofree if:
- it has failed to free
min_free_slots, and - it is a growth heap (ie. it has no empty pages, or it freed more than empty), and
- it doesn't already have enough
allocatably_bytesto cover the shortfall, and - either:
- it has ended at least
RVALUE_OLD_AGEminors since the last major in this state (this allows new objects to age out so a major is worth it), or - the old generation has grown by more than
GC_HEAP_NOFREE_OLDOBJECT_FACTORsince the last major (old objects are piling up, so there is likely promoted garbage that only a major can reclaim)
- it has ended at least
The heap that requests the major resets its own deficits_since_major straight away, and every major GC (whatever triggered it) resets deficits_since_major for all heaps.
If a heap finishes a sweep and the above conditions are not met then we just expand allocatable_bytes unless it already covers the shortfall, and allow the heap to grow.
This essentially turns the "at least `RVALUE_OLD_AGE GCs" check from a single objspace global to a per-heap measurement.
This PR has removed RUBY_GC_HEAP_FREE_SLOTS. This has been around since before the VWA work for 3.1 introduced multiple heaps, and was used to set the minimum number of free slots for the whole objspace.
It was used in gc_marks_finish to decide whether to grow or run a major. Now that each heap makes this decision for itself, using RUBY_GC_HEAP_FREE_SLOTS_MIN_RATIO I chose to remove it rather than keep an environment variable that silently does nothing.
Applications that used it to keep extra headroom can get similar behaviour per heap by raising RUBY_GC_HEAP_FREE_SLOTS_MIN_RATIO or RUBY_GC_HEAP_INIT_BYTES.
Evaluation¶
We compared the PR against its base in two settings. First, ruby-bench headline benchmarks running on a dedicated bare-metal host set up for benchmarking. Then we a/b tested a replay of production traffic against a staging instance of one of our larger production Ruby applications at Shopify.
ruby-bench¶
Headline suite. We ran 3 runs of the headline benchmarks and averaged the results. We saw some speedups and some slowdowns, mostly within variance. Most benchmarks moved by less than 2%. erubi-rails was 2.2% faster, and four benchmarks were a little slower:
mail: −4.1%sequel: −3.2%liquid-render: −2.2%psych-load: −1.9%
All four are small, at around 40 MiB RSS, and none of them run major GCs at steady state, so the new trigger logic has nothing to act on. With the patch, these benchmarks ran with a denser heap (fewer, tightly packed pages). That meant 2–4× more minor GCs, but also 12–21% lower RSS.
Output from one representative run:
-------------- ------------- ------------- -------------- -----------
bench base (ms) branch (ms) branch 1st itr base/branch
activerecord 312.3 ± 0.4% 308.0 ± 0.4% 1.027 1.014
chunky-png 887.5 ± 0.3% 890.9 ± 0.1% 1.003 0.996
erubi-rails 1470.4 ± 0.2% 1437.4 ± 0.2% 1.022 1.023
hexapdf 2766.7 ± 0.6% 2749.8 ± 0.4% 0.981 1.006
liquid-c 70.7 ± 1.7% 71.3 ± 1.1% 1.002 0.992
liquid-compile 60.5 ± 1.3% 61.6 ± 1.7% 0.936 0.982
liquid-il 401.4 ± 0.6% 400.8 ± 0.4% 1.002 1.001
liquid-render 177.2 ± 0.7% 181.3 ± 0.4% 1.018 0.978
lobsters 1024.4 ± 0.7% 1007.7 ± 0.3% 1.018 1.017
mail 140.9 ± 0.9% 146.8 ± 0.5% 0.991 0.959
psych-load 2339.6 ± 0.9% 2400.6 ± 1.0% 0.980 0.975
railsbench 2661.6 ± 0.1% 2651.9 ± 0.1% 1.006 1.004
rubocop 178.0 ± 0.7% 177.0 ± 0.6% 1.036 1.006
ruby-lsp 181.4 ± 0.7% 179.1 ± 0.8% 1.059 1.013
sequel 77.5 ± 0.9% 80.1 ± 0.6% 0.977 0.967
shipit 1910.3 ± 1.2% 1922.4 ± 1.2% 0.974 0.994
-------------- ------------- ------------- -------------- -----------
GC time ratios:
-------------- ------- ----- --------- ---------- ------- --------
bench gc/iter gc/GC mark/iter sweep/iter mark/GC sweep/GC
activerecord 1.200 1.200 0.937 1.355 0.937 1.355
chunky-png 0.905 0.880 1.000 0.905 0.971 0.879
erubi-rails 1.181 1.094 1.188 1.182 1.100 1.095
hexapdf 1.037 1.128 1.072 0.981 1.166 1.067
liquid-c 0.848 1.665 0.551 0.887 1.082 1.742
liquid-compile 0.844 1.363 0.695 0.890 1.123 1.437
liquid-il 0.943 1.204 1.000 0.939 1.278 1.200
liquid-render 0.565 2.232 0.366 0.594 1.446 2.347
lobsters 1.096 1.096 1.171 1.076 1.171 1.076
mail 0.659 2.267 0.341 0.682 1.172 2.346
psych-load 0.645 0.966 0.260 0.781 0.390 1.169
railsbench 1.037 0.924 1.000 1.038 0.891 0.925
rubocop 1.001 1.094 0.893 1.033 0.977 1.130
ruby-lsp 0.936 0.936 0.722 1.082 0.722 1.082
sequel 0.686 2.310 0.375 0.702 1.264 2.365
shipit 1.056 0.771 1.050 1.063 0.766 0.776
-------------- ------- ----- --------- ---------- ------- --------
GC counts:
-------------- ------------- ------------- ------------- -------------
bench GCs/iter major/iter minor/iter minor GC %
activerecord 10.0 → 10.0 0.0 → 0.0 10.0 → 10.0 100% → 100%
chunky-png 3.5 → 3.4 0.0 → 0.0 3.5 → 3.4 100% → 100%
erubi-rails 29.9 → 27.7 0.0 → 0.0 29.9 → 27.7 100% → 100%
hexapdf 11.4 → 12.4 2.0 → 1.0 9.4 → 11.4 82% → 92%
liquid-c 0.7 → 1.4 0.0 → 0.0 0.7 → 1.4 100% → 100%
liquid-compile 0.8 → 1.4 0.0 → 0.0 0.8 → 1.4 100% → 100%
liquid-il 3.6 → 4.6 0.0 → 0.0 3.6 → 4.6 100% → 100%
liquid-render 1.8 → 7.2 0.0 → 0.0 1.8 → 7.2 100% → 100%
lobsters 0.9 → 0.9 0.0 → 0.0 0.9 → 0.9 100% → 100%
mail 2.9 → 9.8 0.0 → 0.0 2.9 → 9.8 100% → 100%
psych-load 33.4 → 50.0 0.0 → 0.0 33.4 → 50.0 100% → 100%
railsbench 11.0 → 9.8 0.0 → 0.0 11.0 → 9.8 100% → 100%
rubocop 1.6 → 1.7 0.0 → 0.0 1.6 → 1.7 100% → 100%
ruby-lsp 1.0 → 1.0 0.0 → 0.0 1.0 → 1.0 100% → 100%
sequel 1.2 → 4.0 0.0 → 0.0 1.2 → 4.0 100% → 100%
shipit 6.3 → 4.6 0.2 → 0.2 6.1 → 4.4 97% → 96%
-------------- ------------- ------------- ------------- -------------
Hidden columns (zero or N/A in every row): global/iter
Legend:
- branch 1st itr: ratio of base/branch time for the first iteration.
- base/branch: ratio of base/branch time. Higher is better for branch. Above 1 represents a speedup.
- GC time ratios are base/comparison; above 1 means the comparison spent less GC time.
- gc/iter, mark/iter, and sweep/iter compare total GC (or phase) time per benchmark iteration, so they include both per-GC cost and GC frequency changes.
- gc/GC, mark/GC, and sweep/GC divide average GC (or phase) time by the same run's GCs/iter count; they are not complete per-cycle attribution of process-wide GC.
- GC counts show base → comparison values, not ratios. Rows with no GC activity are omitted.
hexapdf. We looked at hexapdf in more detail because it is the headline benchmark with the most major GCs at steady state. We used GC::Profiler to count the number of majors and their reasons:
nofreemajors fell from 23 to 4 (−83%)oldgenmajors rose from 22 to 27, as the old-generation heuristic picked up some of the cases where promoted garbage had built up- total majors fell from 46 to 32 (−30%)
Across seven gc-harness runs (the four full-suite runs above and three dedicated hexapdf runs):
- majors per iteration fell in every run, from 1.2–2.0 to 1.0
- total GC time per iteration was lower in six of the seven runs, by 0.4–9%, mostly from less marking; one run was about 5% higher
- peak RSS did not change (299–319 MiB before, 307–317 MiB after, over the three dedicated runs)
- wall time was 0.7% faster over the three dedicated runs, which is within the noise
Production application¶
We deployed both builds to staging instances of one of Shopifys larger Ruby apps and replayed the same production traffic to both: about 1.26M requests each over a time period of about 3 hours and observed the following:
- about 6% fewer major GCs in total
- remaining majors that were 17% shorter on average
- about 16% less total GC time
- no change in peak RSS per worker
- no measurable change in request latency
Conclusions¶
On workloads that run major GCs regularly this change reduces major GCs and total GC time without increasing peak memory. Even though hexapdf and our internal app differ in heap size by more than an order of magnitude you can see the same effect clearly in both tests.
There is a small cost that appears in small workloads that never reliably run major GC. We suspect that this is due to the removal of free-slot headroom that was either a fixed size or derived from the initial heap size (the RUBY_GC_HEAP_FREE_SLOTS floor, and the init_slots term in min_free_slots).
No data to display