Video summary

The Latency Slippage: Understanding the Hidden Costs of Caching for Low Latency C++ - Sanchit Gupta

Main summary

Key takeaways

Technology

Summary

Sanchit Gupta presents a practical look at cache-related latency in C++, using an options-pricing workload on an Intel i9 Raptor Lake server. He emphasizes that cache optimization involves more than arranging data contiguously: hardware must also predict what to fetch, determine which core will own a cache line, and decide what to retain or evict. Because results depend on the microarchitecture, he recommends measuring each optimization on the target system.

Workload and memory-level parallelism

The example processes market-data updates, repricing 64 dependent options per stock update. The application’s working set is slightly larger than the machine’s L3 cache, so some accesses miss there. The options are independent of one another, making it possible to overlap their memory accesses.

The first optimization batches option processing rather than handling each option’s potential cache miss serially. This improves latency up to a batch size of about four; larger batches eventually perform worse. The limiting factor is the core’s finite number of fill buffers, which track outstanding cache-line requests. In this test, performance gains tapered as the workload approached the server’s measured capacity of at least 16 fill buffers.

Takeaway: Batching can improve memory-level parallelism when an application is serializing misses, but it stops helping when outstanding requests are limited by hardware resources.

Software prefetching

Because the application knows which options will be needed after receiving a stock update, Gupta tests prefetching the next batch while computing on the current one. On this workload, an L1-oriented T0 prefetch improves latency by roughly 3%—about 15 ns—over batching alone. The best measured configuration is again around a batch size of four.

The talk compares prefetching with loading data into a dummy register. A prefetch can retire without waiting for the data, while a load must wait for the cache line, so the load variant shows more stalls. Non-temporal prefetching also performs poorly in this case because the data is reused on subsequent market-data events.

Prefetching is not free: requests consume fill-buffer capacity and can train the hardware prefetcher. Fetching data that is not used can waste cache space and evict useful lines. The right distance, hint, and batch size depend on the workload and should be benchmarked.

Managing cache eviction

Gupta next tests CLFLUSHOPT to evict data that has been read once, creating cache capacity for more useful lines. In the main application, it yields a modest latency improvement—about 5 ns—and improves some hardware-counter measures. A separate test shows that it helps when cache pressure is high, but not when the hot working set already fits in L2. The strongly ordered CLFLUSH variant performs worse in the test, and flushing also uses fill-buffer resources.

Takeaway: Explicit eviction may help with genuinely one-use data under cache pressure, but it can add overhead and compete with other memory requests.

Cross-core cache coherence

The talk then moves to a two-core version of the workload. A main core handles event processing and sends a request to an auxiliary core to price some of the options. Gupta finds that accessing a value from a shared request object can be more expensive than using a local copy. In the test, changing to the local copy reduces latency by about 20 ns.

The explanation is cache coherence: when another core has a cache line, reading or writing that line can require coherence traffic and invalidate another core’s copy. The measured penalty is particularly significant when a core must reacquire a line that was modified elsewhere. Gupta also reports that, on the tested system, a dirty line evicted from L2 could bypass L3 and go to memory, making a later cross-core read much more expensive. He cautions that the behavior is architecture- and access-pattern-specific.

He tests CLWB as a way to write back a dirty line before another core needs it. In the application, writing back the auxiliary core’s results reduces total latency by about another 20 ns. The talk’s broader recommendation is to minimize shared cache-line traffic and arrange handoffs so each core reads shared data as few times as possible.

Results and practical guidance

Combining the optimizations reduces the example’s processing time from approximately 500 ns to 330 ns, without changing the underlying option-pricing computation.

The speaker’s main recommendations are:

  • Profile before optimizing, and use hardware counters to identify the actual bottleneck.
  • Re-profile after each change, since optimizations can shift the bottleneck.
  • Use batching and prefetching when the application can overlap memory misses, while accounting for fill-buffer limits.
  • Consider cache flushing for one-use data only when profiling indicates cache pressure.
  • Minimize cross-core sharing and coherence traffic.
  • Verify generated assembly when relying on compiler intrinsics or expecting particular instructions.
  • Re-test tuning parameters such as batch size after other code changes.

In the Q&A, Gupta mentions cycle timing with RDTSC and hardware performance counters. He also points to Intel VTune and top-down microarchitecture analysis for broader bottleneck diagnosis. He notes that SIMD computation does not by itself change memory-level parallelism when it only changes the arithmetic, though it can matter if it changes memory accesses.

Main speakers and sources

  • Sanchit Gupta — main speaker and presenter.
  • CppCon moderator and audience members — ask questions during the Q&A.
  • An attendee — provides a brief introductory comment before the talk begins.

Rate this summary

Your feedback will help improve summaries.

Improve this summary

Reprocess with a stronger model when the summary feels incomplete or inaccurate.

Pro

Translate summary in another language

Pro

Ask questions to this video

Chat for follow-up questions, clarifications, and source-backed answers.

Coming soon

Share this summary

Original video