The cost of scanning memory in C++

A game needs to count its active entities. Each record occupies 64 bytes, and the answer for each entity fits in one. How much time could the loop save by reading just that byte?

One count, two layouts

In the first version, a vector holds 64-byte records. The loop reads the first byte of each record and adds a zero or a one. In the second version, a separate vector holds one byte per entity. Both scans visit the same flags in the same order.

// One flag every 64 bytes
for (const auto& entity : records) {
    count += entity.active;
}

// One flag per byte
for (const auto flag : flags) {
    count += flag;
}

The processor fetches memory in blocks called cache lines. With a 64-byte line, the first loop uses one flag per line and the second can use 64. Both access patterns are regular enough for a hardware prefetcher to follow. What changes is how much of each line the loop uses before moving on.

A vector of pointers to entities would add another variable. Reading the next flag would require loading an address and then following it. If the objects span many pages, address translations can also miss in the processor’s translation lookaside buffer (TLB). Keeping both inputs contiguous lets us compare the distance between flags without introducing that indirection.

Measuring the scan

The program generates 4,194,304 flags with a fixed seed. The records occupy 256 MiB and the separate flags occupy 4 MiB. Each run warms up both scans once, then times twelve pairs on a single thread, alternating which scan goes first. Before each pair it toggles one flag. It checks each result against an independent count and keeps setup and validation outside the timed interval.

Median milliseconds per scan in three runs
LayoutRun 1Run 2Run 3
64-byte records4.7044.8124.699
Contiguous flags1.0471.0241.011

The runs used an AMD Ryzen 7 8700G on Windows 11, compiled for x64 with MSVC 19.51.36256. To repeat the measurement, download the complete C++ source and run these commands in a Visual Studio x64 Native Tools command prompt.

cl /O2 /EHsc /std:c++17 flag-scan.cpp
.\flag-scan.exe

Separating the flags saved about 3.7 ms per count on this machine. It also changed the working set size, and the compiler may vectorize the loops differently. Attributing the difference to cache misses or SIMD would require hardware counters or inspection of the generated code. If the next step needs each entity’s position and velocity, measure that entire pass before changing the game’s layout.

When several cores write to one cache line

The benchmark runs on one thread. With multiple cores, another question matters. Which core writes to each line? If one core writes a field while another reads a neighboring field on the same line, cache coherence can force the cores to exchange that line. This contention between distinct fields is called false sharing.

Netflix found this pattern in the JVM while investigating why some nodes in a service handled fewer requests. A frequently updated field shared a line with a list of superclasses read by other threads. Adding padding between the two fields eliminated the group of slow nodes. Even then, throughput stalled at about 150 requests per second per node.

A second measurement showed that several cores were still updating the same cache field. This was true sharing. Disabling those writes raised throughput to about 350 requests per second per node at the same 55% CPU utilization target. The reported 3.5× gain compares that throughput with the original result for this workload on an m5.12xl instance. The team measured latency separately. The Netflix team’s measurements and JVM patches.