Size-Specialized Memory Allocation
2 days ago
- Go 1.27 introduces faster memory allocation for sizes ≤80 bytes, improving allocation speed by 20-30% and making allocation-heavy programs up to 1% faster.
- The runtime adds specialized allocation functions for each span class (size class combined with pointer/no-pointer info), which are simpler and faster than the generic mallocgc.
- Specialized functions fall back to generic routines for corner cases, and the compiler can call them directly when the allocation size is known at compile-time.
- Key optimizations include faster memory clearing for constant sizes (avoiding function call overhead), manual inlining of helpers, and reduced span class calculations.
- The cutoff at 80 bytes balances performance gains against instruction cache pressure and code size, with the biggest benefits for common sizes like 16 and 24 bytes (interfaces, strings, slices).
- The specialized functions are generated using an inliner tool (go/ast) to maintain consistency and reduce maintenance burden.
- Users get the improvement automatically by building with Go 1.27, and can disable it with GOEXPERIMENT=nosizespecializedmalloc if needed.