Skip to content

Reduce contention on the bdwgc global lock - #608

Merged
edolstra merged 1 commit into
mainfrom
more-per-thread-freelists
Aug 25, 2026
Merged

Reduce contention on the bdwgc global lock#608
edolstra merged 1 commit into
mainfrom
more-per-thread-freelists

Conversation

@edolstra

@edolstra edolstra commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

Motivation

Taken from #597.

Profiling 'nix search nixpkgs --no-eval-cache' showed that ~65% of all futex waits came from the Boehm GC, the largest share being the global allocation lock (GC_allocate_ml): with the default GC_TINY_FREELISTS of 25, only allocations up to 384 bytes are served from the per-thread freelists, and everything larger takes the global lock. Nixpkgs evaluation does ~755k such allocations per search — e.g. a typical derivation attrset (~46 attrs) is a 752-byte Bindings, of which one is allocated per package — all serialized on the lock across the worker threads.

Building bdwgc with GC_TINY_FREELISTS=96 (thread-local up to 1520 bytes) eliminates 93% of the global-lock allocations (755k -> 56k; what remains is essentially the >16 KiB attrset giants), halves the context-switch count at 8-16 workers, and makes the search ~10% faster at 24 workers (~3.7s -> ~3.3s). The per-thread memory cost is at most a partial heap block per size class and object kind.

Assisted-by: Claude Fable 5 noreply@anthropic.com

Context

Summary by CodeRabbit

  • Bug Fixes
    • Improved memory-management behavior by tuning tiny freelist allocation thresholds.
    • Adjusted heap-growth validation to account for increased thread-local free-list retention and fragmentation.
    • Retained existing batch allocation improvements for more reliable garbage-collector behavior.

Profiling 'nix search nixpkgs --no-eval-cache' showed that ~65% of all
futex waits came from the Boehm GC, the largest share being the global
allocation lock (GC_allocate_ml): with the default GC_TINY_FREELISTS
of 25, only allocations up to 384 bytes are served from the per-thread
freelists, and everything larger takes the global lock. Nixpkgs
evaluation does ~755k such allocations per search — e.g. a typical
derivation attrset (~46 attrs) is a 752-byte Bindings, of which one is
allocated per package — all serialized on the lock across the worker
threads.

Building bdwgc with GC_TINY_FREELISTS=96 (thread-local up to 1520
bytes) eliminates 93% of the global-lock allocations (755k -> 56k;
what remains is essentially the >16 KiB attrset giants), halves the
context-switch count at 8-16 workers, and makes the search ~10% faster
at 24 workers (~3.7s -> ~3.3s). The per-thread memory cost is at most
a partial heap block per size class and object kind.

Also add a patch (from the bdwgc "nix-patches" branch) that makes
gctest's fixed "unexpected heap growth" limit scale with
GC_TINY_FREELISTS, since the larger freelists exceed it by design.

Assisted-by: Claude Fable 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Aug 25, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fc371d0f-48ed-45b5-90a6-6a8fae1c2305

📥 Commits

Reviewing files that changed from the base of the PR and between 3588ecb and 0a28caa.

📒 Files selected for processing (2)
  • packaging/dependencies.nix
  • packaging/patches/boehmgc-gctest-tiny-freelists-heap-growth.patch

Included review availability: 3 reviews are currently available. Your included PR review attempts over the past 7 days set your current allowance at 5 reviews per hour.


📝 Walkthrough

Walkthrough

The BoehmGC override now enables GC_TINY_FREELISTS=96, retains batch allocation, applies a heap-growth test patch, and scales the test limit for larger tiny freelists.

Changes

BoehmGC tiny freelists

Layer / File(s) Summary
Heap-growth test adjustment
packaging/patches/boehmgc-gctest-tiny-freelists-heap-growth.patch
The test scales max_heap_sz by GC_TINY_FREELISTS / 25 when the value exceeds 25.
BoehmGC build configuration
packaging/dependencies.nix
The override applies the new test patch and sets GC_TINY_FREELISTS=96. The existing batch-allocation patch remains enabled.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 0a28c

This localized build configuration change adjusts Boehm GC freelists to reduce allocation contention without any identified product or production-impacting defect; no actionable merge-blocking risk remains after normal checks and review.

Suggested reviewers: xokdvium

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the primary change: increasing Boehm GC freelists to reduce contention on the bdwgc global allocation lock.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0 files. (2 skipped: 2 unsupported.)

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch more-per-thread-freelists

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown

@github-actions
github-actions Bot temporarily deployed to pull request August 25, 2026 16:07 Inactive
@edolstra
edolstra enabled auto-merge August 25, 2026 16:16
@edolstra
edolstra added this pull request to the merge queue Aug 25, 2026
Merged via the queue into main with commit 63b819b Aug 25, 2026
75 of 95 checks passed
@edolstra
edolstra deleted the more-per-thread-freelists branch August 25, 2026 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants