Optimizing Heap Allocations in Go: A Case Study
Published on: 2025-08-14 12:53:32
I work on Dolt, the first SQL database with git-like version control, written entirely in Go. And as a rule, databases need to be fast. So we have lots of tests in our CI workflow to monitor performance regressions before they ever hit our main branch.
Last month, a commit that was supposed to be a no-op refactor caused a 30% regression in sysbench's types_scan benchmark.
Pictured: An example of what you don't want to see on your GitHub PR.
Uncovering the root cause of this regression turned out to be quite illuminating in how Golang handles memory allocations, and I want to share it here as an exercise. The details have been simplified, but the lessons are real.
The change has to do with a type called ImmutableValue , which represents a content hash, an optional byte buffer containing the data with that hash, and a ValueStore interface that can resolve content hashes to binary blobs. ImmutableValue has a GetBytes method that checks if the blob has already been loaded, uses the Val
... Read full article.