Skip to content

Skip redundant cache entry access writes on cache hits - #10618

Draft
tobias-tengler wants to merge 1 commit into
mainfrom
tte/cache-hit-access-flag
Draft

tobias-tengler wants to merge 1 commit into
mainfrom
tte/cache-hit-access-flag

Conversation

@tobias-tengler

@tobias-tengler tobias-tengler commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Every hit in Cache<TValue> and Cache<TKey, TValue> writes the CLOCK access flag of the entry, even when the flag is already set. The document and operation plan caches are hit several times per request, so every core keeps writing to the same hot entries.

This PR reads the flag first and writes it only when it was cleared by the eviction sweep, so hits on an already marked entry no longer write to shared memory. Eviction behavior is unchanged.

Method Version Mean Ratio Allocated Alloc Ratio
Contended cache hit main 5.319 ± 0.068 ns 1.00 0 B NA
Contended cache hit PR 1.147 ± 0.068 ns 0.22 0 B NA

Four threads hit the same entry of a Cache<string>.

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Patch coverage

85.0% of changed lines covered (17/20)

File Covered Changed Patch %
src/HotChocolate/Caching/src/Caching.Memory/Cache`2.cs 6 9 66.7% 🔴
src/HotChocolate/Caching/src/Caching.Memory/Cache.cs 11 11 100.0% 🟢
Uncovered changed lines (JSON)
{
  "sha": "819dbaac7b5ada2277cb1af1aa849d7e9ce64548",
  "files": [
    { "path": "src/HotChocolate/Caching/src/Caching.Memory/Cache\u00602.cs", "ranges": [[299, 301]] }
  ]
}

Project coverage: 59.3% (315102/531566 lines)

Cache hits read the CLOCK access flag before writing it, so hot document and plan
cache entries are no longer written from every core on every request.
@michaelstaib
michaelstaib force-pushed the tte/cache-hit-access-flag branch from 52fe65b to c653bb9 Compare October 22, 2026 23:08

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant