Skip to content

Reorder Map bucket fields to avoid false sharing on writes - #206

Merged
puzpuzpuz merged 4 commits into
mainfrom
map_write_false_sharing
May 23, 2026
Merged

puzpuzpuz merged 4 commits into
mainfrom
map_write_false_sharing

Conversation

@puzpuzpuz

@puzpuzpuz puzpuzpuz commented May 23, 2026 •

Copy link
Copy Markdown
Owner

bucketPadded is exactly 64 bytes (one cache line) and contains GC-relevant pointers, so make([]bucketPadded, n) is allocated via Go's small-object path. For size classes that fall into the [16, 512) bucket range, the Go allocator prefixes the allocation with an 8-byte malloc header, which makes the slice base sit at offset 8 (mod 64) rather than 0. Each bucket
therefore occupies two cache lines.

The fix reorder the fields in bucket so that mu precedes next:

type bucket struct {
    meta    uint64
    entries [entriesPerMapBucket]unsafe.Pointer // *entry
    mu      sync.Mutex
    next    unsafe.Pointer // *bucketPadded
}

Benchmarks

buckets before (old order) after (fixed)
64 ~22 ns ~17 ns (≈ −23%)
128 ~15.4 ns ~13.2 ns (≈ −14%)
256 ~13.3 ns ~11.2 ns (≈ −16%)
512 ~10.7 ns ~10.6 ns
1024 ~10.8 ns ~10.7 ns

The existing BenchmarkMap_WarmUp / BenchmarkMapInt_WarmUp benchmarks use WithPresize(1000), which produces a 512-bucket table - already in the aligned range. They should show no change.

@coderabbitai

coderabbitai Bot commented May 23, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: ba703dc6-c195-41b1-88cd-7290cc8f7fd4

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@codecov-commenter

codecov-commenter commented May 23, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.85%. Comparing base (1d45188) to head (f7479ec).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #206      +/-   ##
==========================================
- Coverage   99.54%   98.85%   -0.69%     
==========================================
  Files           7        7              
  Lines         875      875              
==========================================
- Hits          871      865       -6     
- Misses          4        9       +5     
- Partials        0        1       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@puzpuzpuz
puzpuzpuz merged commit 880af08 into main May 23, 2026
5 checks passed
@puzpuzpuz
puzpuzpuz deleted the map_write_false_sharing branch May 23, 2026 15:26
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