fix: optimize spinlock with exponential backoff - #5602
Conversation
|
Thanks for the PR. I do not think this is worth merging as-is. The submitted benchmark only covers an empty critical section. On my machine it does improve that microbenchmark modestly, but it does not support the claimed 4x-6x high-contention improvement. I also tried the same lock benchmark with a yielding critical section to force waiters to build up, and the change regressed throughput on darwin/arm64:
That makes sense for this implementation: once a goroutine misses the CAS a few times, it can call runtime.Gosched up to 16 times before retrying, which may reduce CAS pressure but can also delay acquisition and hurt throughput under actual contention. Without a benchmark that represents the intended workload and shows a clear win, this feels like trading one narrow microbenchmark improvement for worse behavior in another contended case. |
What type of PR is this?
What this PR does:
Benchmark result
Before (old pure spinlock):
goos: windows
goarch: amd64
pkg: github.com/zeromicro/go-zero/core/syncx
cpu: AMD Ryzen 7 5700X 8-Core Processor
BenchmarkSpinLock
BenchmarkSpinLock-16 236873599 5.066 ns/op
After (optimized with exponential backoff):
goos: windows
goarch: amd64
pkg: github.com/zeromicro/go-zero/core/syncx
cpu: AMD Ryzen 7 5700X 8-Core Processor
BenchmarkSpinLock
BenchmarkSpinLock-16 273461516 4.388 ns/op