Skip to content
IJK

Size-Specialized Allocation in Go: The Assumptions Behind It

By IJK ·

How the Go post says size classes work, the assumptions the specialized allocation functions use and how inlining is overridden in the generated code.

How size classes work

The size is important because the allocator defines ranges of sizes called “size classes”. [1]

For most allocations that are not too large and not too small, it will return a block of memory from a list of free objects that are all sized to the maximum size of the size class. [2]

So whether you allocate 17 bytes or 24 bytes, the allocator will give you the next free 24 byte object available in its list of 24 byte objects. [3]

Requested sizefor example 17 bytesSize classthe range the size falls inNext free object in the classfor example a 24 byte object
How does the allocator turn a requested size into a block?

What the specialized functions assume

These specialized functions can then make certain assumptions that make them faster and easier to optimize. [4]

And because the size of the allocation is a constant, the compiler is able to do some optimizations to speed up the bookkeeping necessary for the allocation. [5]

One such case is with marking where the pointers are in the allocated memory. [6]

How inlining is handled

The specialized functions could also manually inline several of their helper functions. [7]

The Go compiler can inline code, but it avoids inlining functions that it considers too large. [8]

We can override that in the generated code by inserting the function bodies into the callers. [9]

Sources

  1. The Go Team source says: The size is important because the allocator defines ranges of sizes called “size classes”. Size-Specialized Memory Allocation
  2. The Go Team source says: For most allocations that are not too large and not too small, it will return a block of memory from a list of free objects that are all sized to the maximum size of the size class. Size-Specialized Memory Allocation
  3. The Go Team source says: So whether you allocate 17 bytes or 24 bytes, the allocator will give you the next free 24 byte object available in its list of 24 byte objects. Size-Specialized Memory Allocation
  4. The Go Team source says: These specialized functions can then make certain assumptions that make them faster and easier to optimize. Size-Specialized Memory Allocation
  5. The Go Team source says: And because the size of the allocation is a constant, the compiler is able to do some optimizations to speed up the bookkeeping necessary for the allocation. Size-Specialized Memory Allocation
  6. The Go Team source says: One such case is with marking where the pointers are in the allocated memory. Size-Specialized Memory Allocation
  7. The Go Team source says: The specialized functions could also manually inline several of their helper functions. Size-Specialized Memory Allocation
  8. The Go Team source says: The Go compiler can inline code, but it avoids inlining functions that it considers too large. Size-Specialized Memory Allocation
  9. The Go Team source says: We can override that in the generated code by inserting the function bodies into the callers. Size-Specialized Memory Allocation

Editorial (AI-assisted; automatically checked, not human-reviewed). Each statement is linked to its source; check the source before relying on a detail.

  • go
  • runtime
  • memory
  • performance