Go internals and performance
You get to know the scheduler, the garbage collector and escape analysis through measurement and use profiles and traces to find bottlenecks and cut allocations.
Level 501 · 16 lessons · 15 practice labs
Lessons
The runtime
- The scheduler: the G, M and P model
- The garbage collector: GOGC and GOMEMLIMIT
- Escape analysis: stack or heap?
- How interfaces and slices look in memory
- Short task: bring allocations in a hot loop down to zero
Measuring
- CPU, heap and mutex profiles with pprof
- Execution traces with go tool trace
- Profile-guided optimisation (PGO)
- sync.Pool and buffer reuse
- Short task: profile a slow service and make it twice as fast
Beyond the safe boundaries
- The unsafe package: when and why it is dangerous
- Calling C code with cgo
- The cost of cgo: memory safety and distribution
- Inspecting the binary: go version -m and the symbol table
- Short task: a safe Go wrapper for a C library
Capstone
- Capstone: measure a log pipeline, find the bottleneck and halve its memory use
Practice labs
- Reading escape analysis output: Find the variables escaping to the heap from the compiler's escape analysis output and keep one on the stack by returning a value.
- Removing allocations from interface boxing: Replace a logging call that allocates by boxing values into an empty interface in a hot loop with a typed version and measure allocations with a benchmark.
- Bringing allocations in a hot loop to zero: Remove string conversions, slice growth and closure allocations from a line-parsing hot loop until the benchmark shows zero allocations per operation.
- Measuring scaling with processor count: Write a benchmark that runs CPU-bound work with different GOMAXPROCS values and produces a table of timings.
- Tuning memory with GOGC and GOMEMLIMIT: Choose GOMEMLIMIT and GOGC values for a service killed in a memory-limited container, document the choice by measuring collection count and peak memory and stay under the limit.
- Stopping a small slice keeping a large array alive: Show from the heap profile that small sub-slices of a large file keep the whole buffer in memory, fix it with a copy and measure the drop.
- Finding the costliest function from a CPU profile: Take a CPU profile of a slow report generator and document the function spending most time with pprof output.
- Finding lock contention with a profile: Turn on the mutex profile in a service that does not speed up with more cores, find the contended lock and raise throughput by narrowing the critical section.
- Finding the root cause of latency from an execution trace: Find why requests occasionally run long with go tool trace, separate the effects of garbage collection, blocked goroutines and system calls and make the service twice as fast.
- Reusing buffers with sync.Pool: Replace an encoder that allocates a new bytes.Buffer per request with sync.Pool and reset the buffer before putting it back.
- Doing a profile-guided build: Rebuild using a CPU profile from a representative load as default.pgo and report the benchmark gain with benchstat.
- Catching stale data leaking from a pool: Find the content mixing into responses because a reused buffer still holds the previous request's data, fix the pool use and prove it with a concurrent test.
- Inspecting a binary's module and symbol information: Write a small tool that extracts a binary's Go version, modules and build settings from go version -m and the symbol table.
- A safe Go wrapper for a C library: Wrap a small C compression library with cgo, free C memory on every path, check lengths and never keep Go pointers on the C side.
- Curing corruption from an unsafe conversion: Find the corruption caused by a speed trick that converts a string to a byte slice with unsafe and then mutates it, replace it with a safe version and measure the cost.
About this course
You get to know the scheduler, the garbage collector and escape analysis through measurement and use profiles and traces to find bottlenecks and cut allocations.
What you will learn
- The runtime
- Measuring
- Beyond the safe boundaries
- Capstone
Prerequisites
You should be experienced in the topics of the same path up to level 401.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 501; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.