Skip to content

Instantly share code, notes, and snippets.

@davidteather
Last active October 17, 2026 17:39
Show Gist options
  • Select an option

  • Save davidteather/7b0edd767269026c345a721ec4fcc6ca to your computer and use it in GitHub Desktop.

Select an option

Save davidteather/7b0edd767269026c345a721ec4fcc6ca to your computer and use it in GitHub Desktop.
False sharing in Go: packed vs padded counters across 1, 2 and 4 goroutines. Benchmark for dteather.com/blogs/false-sharing.
// Benchmark for https://dteather.com/blogs/false-sharing/
package main
import (
"fmt"
"slices"
"sync"
"sync/atomic"
"time"
"golang.org/x/sys/cpu"
)
const total = 2_000_000
const rounds = 5
type packed struct {
counters [4]atomic.Uint64
}
type padded struct {
counters [4]struct {
v atomic.Uint64
_ cpu.CacheLinePad
}
}
func run(goroutines int, counter func(i int) *atomic.Uint64) time.Duration {
n := total / goroutines
start := time.Now()
var wg sync.WaitGroup
for g := 0; g < goroutines; g++ {
wg.Add(1)
go func(c *atomic.Uint64) {
defer wg.Done()
for range n {
c.Add(1)
}
}(counter(g))
}
wg.Wait()
return time.Since(start)
}
func median(goroutines int, counter func(i int) *atomic.Uint64) time.Duration {
times := make([]time.Duration, rounds)
for i := range times {
times[i] = run(goroutines, counter)
}
slices.Sort(times)
return times[rounds/2]
}
func main() {
var p packed
var q padded
fmt.Println("goroutines packed padded")
for _, g := range []int{1, 2, 4} {
packedTime := median(g, func(i int) *atomic.Uint64 { return &p.counters[i] })
paddedTime := median(g, func(i int) *atomic.Uint64 { return &q.counters[i].v })
fmt.Printf("%-11d %-11v %v\n", g, packedTime.Round(100*time.Microsecond), paddedTime.Round(100*time.Microsecond))
}
}
// Benchmark for https://dteather.com/blogs/false-sharing/
//
// go mod init bench && go get golang.org/x/sys && go test -bench . -benchtime 2s -count 3
package bench
import (
"sync"
"sync/atomic"
"testing"
"golang.org/x/sys/cpu"
)
const total = 2_000_000
type packed struct {
counters [4]atomic.Uint64
}
type padded struct {
counters [4]struct {
v atomic.Uint64
_ cpu.CacheLinePad
}
}
func run(b *testing.B, goroutines int, counter func(i int) *atomic.Uint64) {
n := total / goroutines
for i := 0; i < b.N; i++ {
var wg sync.WaitGroup
for g := 0; g < goroutines; g++ {
wg.Add(1)
go func(c *atomic.Uint64) {
defer wg.Done()
for range n {
c.Add(1)
}
}(counter(g))
}
wg.Wait()
}
}
func BenchmarkPacked1(b *testing.B) {
var p packed
run(b, 1, func(i int) *atomic.Uint64 { return &p.counters[i] })
}
func BenchmarkPacked2(b *testing.B) {
var p packed
run(b, 2, func(i int) *atomic.Uint64 { return &p.counters[i] })
}
func BenchmarkPacked4(b *testing.B) {
var p packed
run(b, 4, func(i int) *atomic.Uint64 { return &p.counters[i] })
}
func BenchmarkPadded1(b *testing.B) {
var p padded
run(b, 1, func(i int) *atomic.Uint64 { return &p.counters[i].v })
}
func BenchmarkPadded2(b *testing.B) {
var p padded
run(b, 2, func(i int) *atomic.Uint64 { return &p.counters[i].v })
}
func BenchmarkPadded4(b *testing.B) {
var p padded
run(b, 4, func(i int) *atomic.Uint64 { return &p.counters[i].v })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment