hn.today

Size-Specialized Memory Allocation

go.dev12 points3 comments
Screenshot of Size-Specialized Memory Allocation

Go 1.27 speeds up heap allocation for objects of 80 bytes or less by adding size- and pointer-specialized allocation routines, yielding up to 20-30% faster small allocations and overall program speedups around 1% for allocation-heavy code. Allocations are organized into size classes (1:1-8, 2:9-16, 3:17-24, 4:25-32, 5:33-48, 6:49-64, 7:65-80) and separate free lists for pointer vs non-pointer objects; the runtime encodes both into a span class (sizeClass<<1 | noPointers). New specialized mallocgc variants (e.g., mallocgcSmallNoScanSC3) handle the tiny case and each non-tiny span class, and the compiler can call these directly when it knows the span class at compile time. The biggest wins are for common 16- and 24-byte allocations (interfaces, strings, slices).

Performance comes mainly from exploiting constant-size clears so the compiler emits inline clear instructions instead of calling memclr, and from removing span-class computation, inlining helper logic, and moving uncommon checks into slow paths. A code-generator based on go/ast produces consistent specialized functions to avoid maintenance drift. Tradeoffs include increased binary/code-cache footprint, so specialization stops at 80 bytes to minimize icache pressure. Specialized paths fall back to the generic allocator for corner cases (GC active, runtime flags). To use it, build with Go 1.27; disable via GOEXPERIMENT=nosizespecializedmalloc if necessary.

Read on go.dev3 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.