diff options
| author | “Muhammad <hamza3256@live.co.uk> | 2026-03-01 23:53:03 +0000 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2026-03-04 11:13:38 -0800 |
| commit | 50126a8e44f76134349edf5ba3cc94efabc61c80 (patch) | |
| tree | 1b4110fadab2962c3250ce844a0e2e8f6898e003 /src/runtime | |
| parent | 7aeb2f7e285006ad68789581dedf2b35ba444dbf (diff) | |
| download | go-50126a8e44f76134349edf5ba3cc94efabc61c80.tar.xz | |
runtime, cmd/compile: use preemptible memclr for large pointer-free clears
Large memory clearing operations (via clear() or large slice allocation)
currently use non-preemptible assembly loops. This blocks the Garbage
Collector from performing a Stop The World (STW) event, leading to
significant tail latency or even indefinite hangs in tight loops.
This change introduces memclrNoHeapPointersPreemptible, which chunks
clears into 256KB blocks with preemption checks. The compiler's walk
phase is updated to emit this call for large pointer-free clears.
To prevent regressions, SSA rewrite rules are added to ensure that
constant-size clears (which are common and small) continue to be
inlined into OpZero assembly.
Benchmarks on darwin/arm64:
- STW with 50MB clear: Improved from 'Hung' to ~500µs max pause.
- Small clears (5-64B): No measurable regression.
- Large clears (1M-64M): No measurable regression.
Fixes #69327
Change-Id: Ide14d6bcdca1f60d6ac95443acb57da9a8822538
Reviewed-on: https://go-review.googlesource.com/c/go/+/750480
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
Diffstat (limited to 'src/runtime')
| -rw-r--r-- | src/runtime/malloc.go | 9 | ||||
| -rw-r--r-- | src/runtime/memmove_test.go | 25 |
2 files changed, 34 insertions, 0 deletions
diff --git a/src/runtime/malloc.go b/src/runtime/malloc.go index c08bc7574b..2144ea602a 100644 --- a/src/runtime/malloc.go +++ b/src/runtime/malloc.go @@ -2202,6 +2202,15 @@ func memclrNoHeapPointersChunked(size uintptr, x unsafe.Pointer) { } } +// memclrNoHeapPointersPreemptible is the compiler-callable entry point +// for clearing large buffers with preemption support. It has the same +// signature as memclrNoHeapPointers so the compiler can emit calls to it +// directly. It delegates to memclrNoHeapPointersChunked which splits the +// work into 256KB chunks with preemption checks between them. +func memclrNoHeapPointersPreemptible(ptr unsafe.Pointer, n uintptr) { + memclrNoHeapPointersChunked(n, ptr) +} + // implementation of new builtin // compiler (both frontend and SSA backend) knows the signature // of this function. diff --git a/src/runtime/memmove_test.go b/src/runtime/memmove_test.go index 6065a84553..292dd0f686 100644 --- a/src/runtime/memmove_test.go +++ b/src/runtime/memmove_test.go @@ -1374,3 +1374,28 @@ func BenchmarkMemmoveKnownSize1024(b *testing.B) { memclrSink = p.x[:] } + +func BenchmarkSTWLatency(b *testing.B) { + const bufSize = 50 << 20 // 50 MiB + + buf := make([]byte, bufSize) + var stop atomic.Bool + go func() { + for !stop.Load() { + clear(buf) + } + }() + + var maxPause int64 + for i := 0; i < b.N; i++ { + start := Nanotime() + GC() + elapsed := Nanotime() - start + if elapsed > maxPause { + maxPause = elapsed + } + } + stop.Store(true) + + b.ReportMetric(float64(maxPause)/1e3, "max-pause-µs") +} |
