Skip to content

Make intern.Table completely lock-free#670

Merged
mcy merged 25 commits intomainfrom
mcy/intern-amortize
Mar 25, 2026
Merged

Make intern.Table completely lock-free#670
mcy merged 25 commits intomainfrom
mcy/intern-amortize

Conversation

@mcy
Copy link
Copy Markdown
Member

@mcy mcy commented Mar 5, 2026

This change removes all locks in intern.Table, replacing the map with a sync.Map and the table with an atomicx.Log. This eliminates virtually all lock contention, resulting in wins on all workloads.

The sync.Map is used to ensure that elements are only ever allocated once, using LoadOrStore to select the goroutine responsible for selecting the intern ID for a string. This ID is selected locklessly using atomicx.Log. This makes every operation, except for rare atomicx.Log resizes, lockless.

Here is the before and after benchmarks for a mix of hit-heavy and miss-heavy workloads (Npct means that N% of intern calls are with an uninterned value).

BenchmarkIntern/0pct-10                      361           2840396 ns/op
BenchmarkIntern/10pct-10                     332           3720667 ns/op
BenchmarkIntern/50pct-10                     225           5171647 ns/op
BenchmarkIntern/100pct-10                    230           5382226 ns/op

BenchmarkIntern/0pct-10                     6547            160613 ns/op
BenchmarkIntern/10pct-10                    1818            651644 ns/op
BenchmarkIntern/50pct-10                    1003           1279426 ns/op
BenchmarkIntern/100pct-10                    696           2215831 ns/op

Notice that we see a 17x improvement for a hit-heavy workload, a 4x improvement for the 50% workload. This patch is an unambiguous performance improvement.

To validate this finky change, in addition to the compiler's tests, I've written a test that hammers an intern table directly by simulating a thundering herd.

@mcy mcy changed the title Improve concurrent performance of intern.Table by around 65x Improve concurrent performance of intern.Table by around 15x Mar 6, 2026
@mcy mcy changed the title Improve concurrent performance of intern.Table by around 15x Make intern.Table cache-hit operations lock-free Mar 6, 2026
@mcy mcy changed the title Make intern.Table cache-hit operations lock-free Make intern.Table completely lock-free Mar 7, 2026
@mcy
Copy link
Copy Markdown
Member Author

mcy commented Mar 25, 2026

Ok so I finally go around to doing a macrobenchmark (compiling all of googleapis with sourcecodeinfo output) with this change, and it's about a 20% improvement, so I'm going to merge it.

@mcy mcy enabled auto-merge (squash) March 25, 2026 01:33
@mcy mcy merged commit 23a612b into main Mar 25, 2026
6 checks passed
@mcy mcy deleted the mcy/intern-amortize branch March 25, 2026 01:35
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.

3 participants