Menu

Post image 1
Post image 2
Post image 3
1 / 3
0

Defer Has 3 Performance Cliffs. Here's How to See Them

DEV Community·Gabriel Anhaia·about 1 month ago
#szT8iCWI
#cliff#go#performance#runtime#defer#function
Reading 0:00
15s threshold

Book: The Complete Guide to Go Programming Also by me: Thinking in Go (2-book series) — Complete Guide to Go Programming + Hexagonal Architecture in Go My project: Hermes IDE | GitHub — an IDE for developers who ship with Claude Code and other AI coding tools Me: xgabriel.com | GitHub You profile a hot path. The flame graph shows runtime.deferproc and runtime.deferreturn near the top, eating 8% of the CPU. You did not write either of those. You wrote defer mu.Unlock() and defer f.Close() and went home. Now someone on the team is asking why a function that does almost nothing is the most expensive line in the trace. The answer is that defer has three implementations, not one. The fast one is open-coded defer. It landed in Go 1.14 and runs in around 6ns. The slow one routes through runtime.deferproc , allocates a _defer record, and costs about 35ns. Most of the time you are on the fast path.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More