Repository navigation
Optimize MemoryTracker layout (improves performance of it ~25%) - #103464
Conversation
Group fields into three anonymous nested structs aligned to
`DB::CH_CACHE_LINE_SIZE` by access pattern:
- Hot writes (`amount`, `peak`, `rss`) — updated on every alloc/free
flush.
- Read-mostly (`soft_limit`, `hard_limit`, `profiler_limit`, `parent`,
`metric`, `description_ptr`) — read on every alloc to enforce caps and
walk the tracker chain, but written only at construction or on rare
config changes.
- Cold configuration / sampling parameters.
Previously all fields packed contiguously, so a single cache line held
both `amount` (written on every flush) and the limits (read on every
alloc). Concurrent flushes from many threads invalidated the limit cache
line on every other core, forcing repeated reloads even though those
fields were never written. Splitting onto separate cache lines eliminates
this false sharing on shared trackers (`total_memory_tracker`, hot query
trackers).
`static_assert` on `MemoryTracker` alignment guards against future
reordering that would silently undo the separation.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Merge the previously-separate hot-write group (`amount`, `peak`, `rss`) and read-mostly group (`soft_limit`, `hard_limit`, `profiler_limit`, `parent`, `metric`) into one anonymous struct that occupies exactly 64 bytes — one cache line. `description_ptr` is demoted to the cold group since it is read only on logging paths, not per allocation. Rationale: every field on the alloc/free path now lands on a single cache line, so each allocation needs at most one line in flight. On writer cores the limits/parent/metric come along "for free" with the fetch needed to write `amount`. Across multiple writer cores the line bounces no more than the prior amount-only line did. The trade-off is that reader-only cores re-fetch the line whenever an allocator writes, but allocations dominate over pure reads in practice. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
Workflow [PR], commit [6bce0cc] Summary: ❌
AI ReviewSummaryThis PR reorganizes ClickHouse Rules
Final Verdict
|
LLVM Coverage Report
Changed lines: N/A (no coverable changed lines) · Uncovered code |
e93129b
| struct | ||
| { | ||
| /// Hot writes: updated on every alloc/free flush. | ||
| alignas(DB::CH_CACHE_LINE_SIZE) std::atomic<Int64> amount {0}; |
There was a problem hiding this comment.
A cache coherence size can be larger than a cache line size. Other project uses 128 bytes on x86_64, for example.
There was a problem hiding this comment.
Thanks, this is interesting, but I doubt that it will make any difference, since here we have cold and hot part, hot fits into 64 bytes and cold accessed rarely
Also if we will change it, we need to do some benchmarks, AFAICS it can be helpful for Intel, but not sure about AMD. Though we do not use it for small frequent structures, and if it will help for some CPUs it can be OK to do this unconditionally for amd64.
There was a problem hiding this comment.
I've also tested this changes on Intel XEON, and I don't see the difference - https://pastila.nl/?0012525f/220080b545e4afff7e27c25307341e69#gr1F9PIgEX0ADOBhsmMxFg==GCM
Neither for 64 nor 128 alignment
There was a problem hiding this comment.
I see. I've tried to find a reliable source for my original statement, and it seems to be more complicated: the cache coherence granule is still 64 bytes, not 128, but Intel's spatial prefetcher may load 128 bytes into cache, inducing a false sharing of 128 bytes.
It might be just access-pattern dependent.
There was a problem hiding this comment.
There is MSR setting called "Adjacent Cache Line Prefetcher Disable" or "L2 Adjacent Cache Line Prefetcher Disable" that controls this behavior (see Intel SDM).
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Optimize MemoryTracker layout (improves performance of it ~25%)
Results