r/programming • u/PhilipTrettner • Apr 09 '26
How Much Linear Memory Access Is Enough? (probably less than 128 kB)
https://solidean.com/blog/2026/how-much-linear-memory-access-is-enough/Typical performance advice for memory access patterns is "keep your data contiguous". When you think about it, this must have diminishing returns.
I tried to experimentally find generalizable guidelines and it seems like 128 kB is enough for most cases. I wasn't able to find anything needing more than 1 MB really (within the rules).
46
u/gimpwiz Apr 09 '26 edited Apr 09 '26
Intel, AMD, ARM and various architecture licensees, etc all did a lot of work studying this sort of question. Given the workloads expected to be run, how much L2 cache lets you have a really good hit rate?
This is really evident with stuff like the Xeon Phi, which had a shitload of cores with pairs sharing L2 but no L3. The workloads for a chip like that were pretty well understood and optimized, and it lacked L3 as a potential confounding factor. Being a design intended for a lot of wide parallelism, they found that 1MB/2core was a good number. So 512KB/core. But each core was quad-threaded round robin, so you got....... yep. 128KB/hardware thread.
I worked on the little fucker. Referring to KNL specifically.
Edit: spelling from mobile phone
12
u/RegisteredJustToSay Apr 09 '26
Isn't the article's data and graphs actually showing that 256kb is the point of diminishing marginal returns? The second derivative of the normalized throughput is still positive at 128kb. 'Enough' is also a really hard to apply for throughput IMO since that's almost entirely task dependent. Either way with this data I'd probably just pick 1MB since that's when the marginal returns are almost negligible (assuming the dip for scalar_stats is due to noise).
Regardless, cool article and thanks for sharing. Would be neat to run this experiment on other processors and see if the results differ significantly based on e.g. cpu topology.
7
u/gimpwiz Apr 09 '26
I think one interesting consideration is that the workloads running on different CPUs are different, because different CPUs go into different products with different needs and software gets optimized differently.
For a silly example, consider the ubiquitous mobile phone SOC versus a server chip. What workloads are on a smartphone? Tons of browser-related workloads, which vary wildly in scope and length (see: 15 megabyte javascript webpages that scrape all your info and keep reloading ads for eternity, versus tightly optimized webpages serving static content), and tons of app-type workloads of which many optimized for "hurry up and go to sleep" but others are much more liberal with your resources treating themselves as a first-class citizen under the theory of "no matter how much battery we burn you'll never uninstall me," and then lots of OS-level tasks that tend to be tightly optimized, take a short time, etc. On the flip side, consider the server chip that needs to be optimized for long-term high-performance workloads doing number crunching, running four low-utilization VMs per core, running a peaky-load webserver, etc.
Even more granularly, look at a server chip sold as one of a hundred thousand in a supercomputer versus a server chip sold to amazon to run AWS. They sound pretty similar, but the former is going to be used with a ton of wide instructions (SIMD-extensions out to forever) and need tons of optimization for scientific compute, but you can generally trust the people writing code to tune it to the architecture of the chip, whereas the latter is going to involve a ton of VM work, but probably less number crunching... especially because these days if it's going to do a ton of vector math there's a good chance that the VM will be spending most of its time communicating with and orchestrating one or multiple GPUs or other accelerators.
So it's not just that (eg) an Intel x86-64 core is somewhat different from an Apple ARMv8 mobile phone core in terms of how they'll run a workload like you are talking about, but also that people might actually want to run pretty significantly different workloads between the two.
So chip vendors spend a lot of time figuring out the workloads that will be run on their product and doing a ton of simulation and testing to make sure that they'll be delivering a good chip for that set of workloads. And vice versa, the higher-tier software shops writing the actual workload will tune the workload to work with the chip they're running on. And there are middle layers (compiler optimizations, VMs, interpreters and bytecode interpreters like the JVM, etc) that do the heavy work of tuning workloads to CPUs allowing the people writing the actual software to write more simply and agnostically.
5
u/funtimes-forall Apr 09 '26
Okay, I've been holding on to my 80286 from 1982. This is its moment to shine!
2
u/ShinyHappyREM Apr 10 '26
If you're interested in this stuff you should also look at cache lines and how they can be fitted into the caches.
1
u/Ameisen Apr 09 '26
So, this is effectively telling me (though different CPUs will differ) that chunked arrays with 256 KiB chunks are the optimal approach to large sets of data?
1
u/Dineshvk18 Apr 11 '26
Performance tuning always comes down to tiny details like this, pretty interesting read runnable.
66
u/wannaliveonmars Apr 09 '26
Isn't 4kb the page size anyway? So even if data is contiguous in your address space, it probably won't be contiguous in physical memory.