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]
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
- The Go Team source says: The size is important because the allocator defines ranges of sizes called “size classes”. Size-Specialized Memory Allocation
- 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
- 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
- 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
- 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
- The Go Team source says: One such case is with marking where the pointers are in the allocated memory. Size-Specialized Memory Allocation
- The Go Team source says: The specialized functions could also manually inline several of their helper functions. Size-Specialized Memory Allocation
- 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
- 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