Search code examples
cachingmemoryprocessor

Does memory locality matter for writes?


If I am writing values to memory, and not reading them again, does it have any performance impact if the memory locations are not contiguous? (ie not in the write cache)

Can the processor write them to memory in the background when it has a chance, or does it cause cache problems?


Solution

  • Most caches in CPUs were familiar with are write-back, meaning that writes are not propagated to the next level of the memory hierarchy until there's contention for cache space, and something has to be flushed. If you make a single random write, you're unlikely to see any performance effect, but if you make sustained random writes, you will find that its performance will be significantly lower than sequential writes.

    Making a random write is likely to require allocation of a cache line, because it's likely to be the only thing accessed in that segment of memory. Once you have made enough writes, you'll have filled up the data cache with lines that each contain one valid word, and one of the lines has to be flushed. Because of the way memory is organized and accessed, that flush won't be much faster than flushing an entire line, making that random write be about the same speed as 32 bytes (typical line size) of sequential writes.

    I would recommend writing your own program to test this. Allocate memory much larger than your last-level cache and time the same number of random and sequential 32-bit writes. I'm betting you'll find the random writes to be somewhere between 1/8 and 1/4 as fast as the sequential.

    Another factor, for YOU is the relative balance of random writes to other computation that doesn't access memory. If they're rare, yes, they'll get flushed pretty much in the background. It's only when they become a bottleneck that you'll notice performance impact. The same problem happens with sequential writes, but not as severely.