Search code examples
coptimizationmemory-managementallocationcpu-cache

Is there any benefit to using 4kb allocation pools?


Until recently, I thought CPU caches (at least, some levels) were 4kb in size, so grouping my allocations into 4kb slabs seemed like a good idea, to improve cache locality.

However, reading Line size of L1 and L2 caches I see that all levels of CPU cache lines are 64 bytes, not 4kb. Does this mean my 4kb slabs are useless, and I should just go back to using regular malloc?

Thanks!


Solution

  • 4KB does have a significance: it is a common page size for your operating system, and thus for entries in your TLB.

    Memory-mapped files, swap space, kernel I/Os (write to file, or socket) will operate on this page size even if you're using only 64 bytes of the page.

    For this reason, it can still be somewhat advantageous to use page-sized pools.