How a clever organization of Go structs can save memory
Sometimes I like to take a look at the commit history of various open source projects, applications I use myself or dependencies I use in my applications. Today I was scrolling through the commit history of chi, the HTTP router I use for my blog software.
I came across these two commits: fb48a47641af106c7eae2f09864e7fecd54237bf and bed8648a22121634da5be6019e9223b9630a53bb. And I was wondering about the relationship between struct attribute order and memory in Go.
After some searching I found this simple explanation and understood what’s the problem there.
By default, on a 64 bit architecture 8 bytes are allocated from memory at a time, and there are data types that require 1, 4, or 8 bytes, for example. If the attributes in a struct are arranged inefficiently, it may be that much more memory must be allocated that is not actually needed.
For example, if a bool
(which requires 1 byte) is followed by a float64
(which requires 8 bytes), a new memory block must be allocated for the float64
, resulting in a memory gap of 7 bytes.
By clever arrangement of the attributes memory blocks can be saved, several bool
can share a block.