Skip to content

Reduce lock contention in cache/memory package#3762

Merged
dims merged 1 commit intogoogle:masterfrom
dgrisonnet:cache-reduce-lock-contention
Dec 4, 2025
Merged

Reduce lock contention in cache/memory package#3762
dims merged 1 commit intogoogle:masterfrom
dgrisonnet:cache-reduce-lock-contention

Conversation

@dgrisonnet
Copy link
Contributor

Summary

Replace the global sync.RWMutex-protected map with sync.Map in InMemoryCache to eliminate lock contention during concurrent cache access.

Changes

  • Replace sync.RWMutex + map[string]*containerCache with sync.Map
  • Use Load for lock-free reads in RecentStats
  • Use LoadOrStore pattern in AddStats for atomic container creation
  • Add containerCacheMap typed wrapper to eliminate type assertions at call sites

Benchmark Results

Performance improvement: 2.5x faster for mixed read/write workloads (99% reads, 1% writes)

For the benchmark methodology and detailed performance analysis, see #3756 which applies the same pattern to the manager package.

Replace the global RWMutex-protected map with sync.Map in InMemoryCache.
This provides lock-free reads for the common path (RecentStats lookups)
while maintaining thread-safe writes.

Add containerCacheMap typed wrapper around sync.Map to eliminate
boilerplate type assertions and improve code readability.

Signed-off-by: Damien Grisonnet <dgrisonn@redhat.com>
@haircommander
Copy link
Contributor

LGTM

@dims dims merged commit 317a38e into google:master Dec 4, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants