SpinLock
SpinLock is a spin lock implementation in Go with exponential backoff and adaptive spinning.
Installation
To install the package, run:
go get github.com/daniel-hutao/spinlock
Usage
Import the package and create a new SpinLock:
import "github.com/daniel-hutao/spinlock"
var sl spinlock.SpinLock
Then you can use the Lock
and Unlock
methods:
sl.Lock()
// critical section
sl.Unlock()
We have conducted performance tests to compare the efficiency of our SpinLock implementation with the standard Mutex in Go. The tests were run on a MacBook Pro with an Apple M1 chip, 16GB of RAM.
SpinLock
$ go test -benchmem -run=^$ -bench ^BenchmarkSpinLock$ github.com/daniel-hutao/spinlock
goos: darwin
goarch: arm64
pkg: github.com/daniel-hutao/spinlock
=== RUN BenchmarkSpinLock
BenchmarkSpinLock
BenchmarkSpinLock-10 111107053 10.80 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/daniel-hutao/spinlock 2.973s
Mutex
$ go test -benchmem -run=^$ -bench ^BenchmarkMutex$ github.com/daniel-hutao/spinlock
goos: darwin
goarch: arm64
pkg: github.com/daniel-hutao/spinlock
=== RUN BenchmarkMutex
BenchmarkMutex
BenchmarkMutex-10 10366155 115.5 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/daniel-hutao/spinlock 1.793s
Conclusion
Based on our tests, the SpinLock implementation performs significantly better than the standard Mutex in Go on a MacBook Pro with an Apple M1 chip. Specifically, operations on SpinLock are approximately an order of magnitude faster than those on Mutex.