Proposal: Customizable prefetch strategy for absl::flat_hash_map to avoid cache bouncing on complex keys #2118
Replies: 2 comments
|
It is pretty unlikely that we are going to add a prefetch policy.
As far as I know, |
|
This is peace of code Also for some reasons also Thats why i proposed to define prefetch strategy by the way Occurs imminently. I can say it is good idea to prefetch but it happened too late. Even better to call it before calling find_large. From my pov better to remove this prefetch because perf shows quite big spikes here. may be it will be better. But need to test it carefully |
Uh oh!
There was an error while loading. Please reload this page.
Hi Abseil Team,
We are using absl::flat_hash_map in a performance-critical, highly concurrent in-memory caching system. We have observed a severe performance degradation directly caused by the mandatory PrefetchToLocalCache inside raw_hash_set::prefetch().
After disabling these prefetch hints, we saw a massive improvement in overall map throughput and a drastic reduction in CPU stalls.
Context & Key Layout
Our key type is non-trivial (a flat structure containing an unaligned string pointer or a composite structure):
The Problem
When looking at perf profiling data under heavy workload, the CPU spends a significant amount of cycles stalled directly on the prefetch instructions inside raw_hash_set.h:
Root Cause Analysis
Cache Bouncing & Invalidation: In high-concurrency environments (multiple threads reading/writing to partitioned maps), standard prefetch hints indiscriminately pull control bytes and slot arrays into the local core's L1/L2 cache. If another core is modifying adjacent slots, this triggers immediate line invalidation and heavy cache bouncing.
Speculative Overhead: For non-trivial key types, single explicit cache line reads are often less expensive than constant speculative L1D prefetches that pollute the cache with data that might not be evaluated immediately.
By completely commenting out:
we observed a sharp decline in L1-dcache-load-misses and CYCLE_ACTIVITY.STALLS_L1D_MISS metrics, yielding a significant latency reduction.
Proposal: Customizable Prefetch Policy
Currently, ABSL_HAVE_PREFETCH is an all-or-nothing global macro switcher. We would like to propose a way to customize or completely disable prefetch behavior on a per-map or per-key level.
Option A: Policy template parameter (Preferred C++ way)
Introduce a PrefetchPolicy trait or tag to absl::flat_hash_map:
Where PrefetchPolicy could be absl::no_prefetch_policy to turn prefetch() into a clean no-op for specific high-contention or complex key scenarios.
Option B: Extensible PrefetchTraits based on Key Type
Allow the map to detect if a key type explicitly opts out of prefetch:
I would be happy to share our raw perf / flamegraph data if needed.
What are your thoughts on introducing this flexibility into the container design?
All reactions