Three Quick Go Tips
Recently, my colleague decided to take one of the event processing services and rewrite it from Ruby to Rust. Nobody will be surprised when I write that the processing speed and the memory usage got better by two orders of magnitude.
I was working on the gathering of the cost document for various services and knew that that specific service wasn’t accumulating a lot of cost. I knew the other one, written in Go, was - however - quite expensive. Inspired by my colleague, I figured that - hey - what happens if I rewrite that service to Rust?
There was a surprise - rewriting this specific service made it way faster, but the memory usage barely dropped. I decided to look into the rewritten service and found that the major contributor to the memory usage was processing and allocating of the JSON data. I turned the LLM spin-machine a few times for the optimizations and the results were as good as expected.
Nobody wants, however, to have their service rewritten to another language with a backgrounded LLM though, right? So I figured that maybe I could bring the same class of optimizations to Go, and (spoiler alert) it worked just as well.
The benchmark I ran on the original code, the rewrite and the Go patch showed that not only processing times were within margin of error of Rust’s, but the memory usage - while higher than Rust - was still an order of magnitude smaller than in original code.
I’d say that these optimizations aren’t especially smart or unique, but they were easy to make and thus - I think - worth sharing with others.
Tip 1: Intern your Strings
In the original code the processing happened through various string-to-string maps whose shape was dynamic and so not structurizable, but both keys and values were reasonably discrete1. Each request kept some around until processing was done, and since the service was busy, so was the memory. String interning2 replaced megabytes with bytes making everything smaller (and faster!).
Example below. Note: Agents can handle the refactor for you reasonably safely if you want to try it out.
package main
import "fmt"
type IStr string
// stringInterner hands out one *IStr per unique string.
type stringInterner struct {
cache map[string]*IStr
}
func newStringInterner() *stringInterner {
return &stringInterner{cache: make(map[string]*IStr)}
}
func (s *stringInterner) intern(str string) *IStr {
if p, ok := s.cache[str]; ok {
return p
}
p := new(IStr)
*p = IStr(str)
s.cache[str] = p
return p
}
func (s *stringInterner) internObjects(objs []map[string]string) []map[*IStr]*IStr {
out := make([]map[*IStr]*IStr, len(objs))
for i, obj := range objs {
m := make(map[*IStr]*IStr, len(obj))
for k, v := range obj {
m[s.intern(k)] = s.intern(v)
}
out[i] = m
}
return out
}
func main() {
users := []map[string]string{
{"user_id": "123", "status": "active"},
{"user_id": "456", "status": "active"},
{"user_id": "789", "status": "inactive"},
}
before := 0
for _, u := range users {
for k, v := range u {
before += len(k) + len(v)
}
}
fmt.Println("Original string bytes:", before)
interner := newStringInterner()
interned := interner.internObjects(users)
after := 0
for s := range interner.cache {
after += len(s)
}
fmt.Println("Interned string bytes:", after)
fmt.Println("Unique strings:", len(interner.cache))
// Same pointer shared across objects
statusKey := interner.intern("status")
fmt.Println("Shared 'active' ptr:", interned[0][statusKey] == interned[1][statusKey])
for i, u := range interned {
for k, v := range u {
fmt.Printf("[%d] %s = %s\n", i, *k, *v)
}
}
}Why not
unique.Make?While the unique package is useful, Make is admittedly not quite like Intern for strings, since the Handle[T] is required to keep a string from being deleted from the internal map. This means you need to modify your code to retain handles as well as strings. (The Go Blog)
Tip 2: Forget the photocopies
In Go it’s easier to move structs by value. Not to mention all these pesky nil values you don’t have to worry about. Everyone knows that it costs the memory and it’s as Go obvious as Go obvious it gets. But do you know how much?
For the service I was optimizing it turned out that replacing struct copies with pointers allowed us to cut the number of running Go pods in half and associated infrastructure cost by a significant amount. Neat, right?
Just ask the Agents to find which structs are copied the most, then refactor code to use the references instead. It should be reasonably reviewable and mergeable. And if not - it’s always possible to do this struct-by-struct. And there’s also a Tip 3.
Again, it seems that it optimizes memory only, but with decreased GC pressure and less copying there are more free CPU cycles, so the change touches both memory and processing times.
See for yourself:
package main
import (
"fmt"
"runtime"
)
func heap() uint64 {
runtime.GC()
var m runtime.MemStats
runtime.ReadMemStats(&m)
return m.HeapAlloc
}
func measure(name string, build func() any) uint64 {
before := heap()
data := build()
after := heap()
runtime.KeepAlive(data)
used := after - before
fmt.Printf("%-24s %8.2f MB\n", name, float64(used)/1024/1024)
return used
}
// ---- 1. many orders pointing at few customers ----
type Customer struct {
ID int
Name [64]byte
Address [128]byte
Notes [56]byte
} // 256 bytes
const (
Customers = 1_000
Orders = 100_000
)
type OrderByValue struct {
ID int
Customer Customer // full copy embedded in every order
}
type OrderByRef struct {
ID int
Customer *Customer // 8-byte pointer to a shared customer
}
func ordersByValue() any {
cs := make([]Customer, Customers)
os := make([]OrderByValue, Orders)
for i := range os {
os[i] = OrderByValue{ID: i, Customer: cs[i%Customers]}
}
return os
}
func ordersByRef() any {
cs := make([]Customer, Customers)
os := make([]OrderByRef, Orders)
for i := range os {
os[i] = OrderByRef{ID: i, Customer: &cs[i%Customers]}
}
return struct {
cs []Customer
os []OrderByRef
}{cs, os}
}
// ---- 2. one big config shared by many workers ----
type Config struct {
Name [64]byte
Payload [960]byte
} // 1 KB
type WorkerByValue struct {
ID int
Cfg Config
}
type WorkerByRef struct {
ID int
Cfg *Config
}
const Workers = 10_000
func workersByValue() any {
cfg := Config{}
ws := make([]WorkerByValue, Workers)
for i := range ws {
ws[i] = WorkerByValue{ID: i, Cfg: cfg}
}
return ws
}
func workersByRef() any {
cfg := &Config{}
ws := make([]WorkerByRef, Workers)
for i := range ws {
ws[i] = WorkerByRef{ID: i, Cfg: cfg}
}
return ws
}
func report(a, b uint64) {
fmt.Printf(" saved: %.2f MB (%.1f%%)\n\n",
float64(a-b)/1024/1024, 100*(1-float64(b)/float64(a)))
}
func main() {
fmt.Println("100k orders, 1k customers (256 B each):")
a := measure(" customer by value", ordersByValue)
b := measure(" customer by pointer", ordersByRef)
report(a, b)
fmt.Println("10k workers sharing a 1 KB config:")
c := measure(" config by value", workersByValue)
d := measure(" config by pointer", workersByRef)
report(c, d)
}Of course, you have to estimate struct sizes first. Not every struct is worth a pointer. When in doubt: benchmark and profile.
Tip 3: Go all-in
One of Go’s biggest flaws is that it’s not a Rust (*ducks*), but vanilla Go and After-Static-Analysis-Go are 2 Go’s3.
And thus: make sure to lint the Go code with everything that Go has to offer (today and after this article was written): Bring golangci-lint, enable everything. Bring govulncheck and nilaway. As much as possible.
If your code doesn’t have most of it enabled, it might require a few sweeps to get straight, but it’d be worth it. Adding nilaway showed (thankfully - only) a dozen places in the service where nils weren’t checked, and after further investigation, one place that around twice daily crashed one of the pods.
While the win wasn’t huge (unless you count OCD situation improvement after looking at toothless Grafana charts), it made me much more confident after the above refactorings within the service.
Writing this article took me 4 times as long as getting agents to produce, review, check and integrate the optimizations on a moderately sized codebase. I’m not claiming that these are mind-blowing optimizations, but they are quick and relatively safe to introduce, so worth carrying in a Go toolbox :)
Przemysław Alexander Kamiński
vel xlii vel exlee
Powered by hugo and hugo-theme-nostyleplease.