Matrices and the L1 Cache
Nov 7, 2026
While working on an offshoot project to create my own Go matrix library for a separate clustering project, I ran into unexpected slowdowns when benchmarking my 2D matrix transposition implementation. This is a listing of what happened and how I resolved it. I know C or C++ might be preferred for an investigation like this for more control of the code, but I didn’t want to rewrite it.
Tiling in Matrix Transposition
The most basic approach to transposing a matrix (assuming transposition creates a new matrix, which is helpful in the case of non-square matrices), is to simply iterate over all items in the matrix in row-major order, then insert each one into the result matrix in column-major order.
While this approach is definitely correct, it is inefficient in regards to memory, especially when transposing large matrices. Assume that an matrix is row-major when stored in memory, and a cache line holds less than items. The writes into the result matrix follow column-major order, and so each write requires fetching a new cache line. Once the end of a row in the original matrix is reached, the result matrix jumps to the top of the next column. If is large enough such that the L1 cache cannot hold cache lines, then the initial cache lines have already been evicted by the time they are revisited. Through the entire transposition, the L1 cache is continuously thrashed and provides virtually no benefit towards system performance. The threshold of is also not large; for a 48KiB L1 data cache with a 64-byte cache line, is enough (optimistic even, given that the L1 cache is usually not 100% used, and might be occupied by other values) to cause this behavior.
One common approach to increasing matrix transposition performance is tiling. Rather than transpose entire rows of the matrix at a time, we instead transpose items in submatrices (tiles). The benefit of tiling is that it solves the issue mentioned above. So long as is not too large, the cache lines containing the write-cells of the result matrix can be reused from the L1 cache when a new column is written.
In my testing, I compared different square tile sizes on transposing a matrix, with the following Go benchmark results. C or C++ might be better for more detailed investigations, but this Go benchmark is enough to demonstrate my point.
BenchmarkTransposeSquareTile/1x1 14078752 ns
BenchmarkTransposeSquareTile/2x2 10298875 ns
BenchmarkTransposeSquareTile/4x4 4874728 ns
BenchmarkTransposeSquareTile/8x8 3021990 ns
BenchmarkTransposeSquareTile/16x16 2682364 ns
BenchmarkTransposeSquareTile/32x32 10322786 ns
BenchmarkTransposeSquareTile/64x64 12471176 ns
BenchmarkTransposeSquareTile/128x128 12450942 ns
BenchmarkTransposeSquareTile/256x256 12513642 ns
BenchmarkTransposeSquareTile/512x512 13797633 ns
BenchmarkTransposeSquareTile/1024x1024 13836450 ns
BenchmarkTransposeSquareTile/2048x2048 13978753 ns
We see that using tiles can speed the process up, taking advantage of the L1 cache. As the tile size increases and becomes closer to the original matrix, it is sensible that the runtime becomes approximately equal to the non-tiled implementation. However, there’s a notable threshold (the tile) where the performance suddenly tanks to the non-tiled approach. It turned out that the specific benchmark conditions were the culprit.
Set-Associative Caches
Choosing a matrix to benchmark on brought some unexpected side effects. In particular, the L1 cache of the M4 chip that I ran the benchmarks on has the following properties: 128 KiB size, 128-byte cache lines, and is 8-way set-associative.
An -way set-associative cache is one where the cache itself is divided into sets that each contain entries (cache lines). In my case, I have bytes (1 KiB) per set, and thus 128 sets in the L1 cache. Through some amount of internet research (I think the exact details are Apple’s trade secrets), I believe that the association of cache line to cache set can be simplified to a 1-to-1 static mapping, repeated every cycle of cache lines. This is the common method that is introduced with set-associative caches in many online resources (with no alternatives provided), so I have reason to believe that it is the same used in practice.
The point of the above speculation is that every 128 cache lines, the cache set indexing resets; cache lines in the same position in each 128-line group are assigned to the same cache set. My benchmarking matrix contained 2048 float32s per row (2048-width stride), meaning each row contained 8192 bytes, or 64 cache lines. Thus, accesses to cells in the matrix in every other row in the same column would always map to the same cache set.
In the particular tiling benchmark above, we see improvements through a tile, until attempting a tile, at which point the performance essentially reverts to the no-tiling case. Because the implemented tiling writes in column-major order, it accesses the result matrix’s memory with 64-cache-line stride, and so every access points to one of only two cache sets. Since the cache is 8-way, only 16 cache lines at most can be held in the L1 cache in this scenario.
Therefore, when we expand the tile size beyond , cache lines are constantly evicted, and the behavior essentially becomes that of the initial non-tiled approach.
Note that the performance of the benchmarks after this tile size threshold approaches the no-tiling case gradually. Even though these cases don’t take advantage of the L1 cache, the tiling behavior does still take advantage of the larger L2 and L3 caches, so the performance is still a slight improvement over the non-tiling approach. The fact that the performance gradually slows down is plausibly due to increased TLB misses, where larger tile sizes imply more strided writes, accessing memory that isn’t loaded. This performance decrease is likely overshadowed by the L1 cache performance increases in the smaller cache sizes.
Breaking Data Alignment
The key issue with the benchmarks, as demonstrated above, was that the data was aligned to the cache line size. Therefore, adding padding to each row of the matrix to break the alignment should improve performance by expanding the program’s cache. After a number of rows, the index of the cache set shifts, granting access to more than just two cache sets.
After adding a padding of 8 float32s in each row, I reran the benchmarks for the following results.
BenchmarkTransposeSquareTileWithPadding/1x1 4195556 ns
BenchmarkTransposeSquareTileWithPadding/2x2 3236897 ns
BenchmarkTransposeSquareTileWithPadding/4x4 2379474 ns
BenchmarkTransposeSquareTileWithPadding/8x8 2184315 ns
BenchmarkTransposeSquareTileWithPadding/16x16 2267864 ns
BenchmarkTransposeSquareTileWithPadding/32x32 2176479 ns
BenchmarkTransposeSquareTileWithPadding/64x64 2115266 ns
BenchmarkTransposeSquareTileWithPadding/128x128 2220082 ns
BenchmarkTransposeSquareTileWithPadding/256x256 2119443 ns
BenchmarkTransposeSquareTileWithPadding/512x512 2142086 ns
BenchmarkTransposeSquareTileWithPadding/1024x1024 2062967 ns
BenchmarkTransposeSquareTileWithPadding/2048x2048 3946514 ns
The performance drop for tile sizes larger than is now gone. Instead, we seem to plateau at a new performance, but this one seems to be bound by factors outside of the L1 cache, since I would expect tile sizes larger than 8 to provide additional benefit. Even the tile shows improvement, as the L1 cache is large enough to fit 1024 cache lines across all of its sets, which is achievable now that the matrix rows are padded. Increasing past that size returns to cache eviction issues, causing performance to drop again. Note that the tile is slightly faster than the no-tiling case due to less loop overhead.
Further Improvements
This blog is long enough as-is, but further improvements are definitely possible. For example, there is nothing requiring the tiles to be square. In fact, non-square tiles are almost definitely an improvement. By having a rectangular tile with many rows, we can take advantage of cache locality during the result matrix writes to further improve performance. For example, the tile showed promise here, but a or even a tile might result in even faster performance.
There are also better approaches for matrix transposition that take advantage of cache locality further, such as during writes, but I did not explore them here, as this was only meant to be a quick test of a method.