/high-performance-go-workshop

https://dave.cheney.net/high-performance-go-workshop/gophercon-2019.html

Primary LanguageGo

Usage

You can view current version this presentation here

License and Materials

This presentation is licensed under the Creative Commons Attribution-ShareAlike 4.0 International licence.

You are encouraged to remix, transform, or build upon the material, providing you give appropriate credit and distribute your contributions under the same license.

Cheat sheet

Benchmarks

Basic command

go test -bench=. -cpu=1,2,4 -run=^$ ./examples/fib/

Comparison with benchstat

go test -c
mv fib.test fib.golden
go test -c
./fib.golden -test.bench=. -test.count=10 > old.txt
./fib.test -test.bench=. -test.count=10 > new.txt
benchstat old.txt new.txt

Look at assembly

go test -gcflags=-S
# disable inlining
go test -gcflags="-l -S"

Fixing compiler optimizations

var Result uint64

func BenchmarkPopcnt(b *testing.B) {
	var r uint64
	for i := 0; i < b.N; i++ {
		r = popcnt(uint64(i))
	}
	Result = r
}

Profiling

Profile benchmark

go test -run=XXX -bench=. -cpuprofile=c.p bytes
go tool pprof c.p

HTTP option (with flame graph mode)

go tool pprof -http=:8080 $FILENAME

Optimizations

Escape analysis & inlining

go build -gcflags=-m examples/esc/sum.go
go build -gcflags='-m -m' examples/esc/sum.go

go build -gcflags=-m examples/inl/max.go

Execution tracer

Get the trace

import "github.com/pkg/profile"

func main() {
	defer profile.Start(profile.TraceProfile, profile.ProfilePath(".")).Stop()
    // ...
}

Analyze the trace

go tool trace trace.out