Skip to content

Replace lock-based stats with Interlocked in MemoryPluginCache#37

Merged
Sarmkadan merged 1 commit into
mainfrom
fix/cache-stats-use-interlocked
May 21, 2026
Merged

Replace lock-based stats with Interlocked in MemoryPluginCache#37
Sarmkadan merged 1 commit into
mainfrom
fix/cache-stats-use-interlocked

Conversation

@Sarmkadan
Copy link
Copy Markdown
Owner

Problem: MemoryPluginCache uses lock(_statsLock) for simple counter increments on the cache hit/miss hot path. This creates unnecessary contention when multiple threads access the cache concurrently.

Solution: Replace lock blocks with Interlocked.Increment for counter updates and Interlocked.Read for statistics retrieval. Remove the _statsLock object entirely.

This is a zero-allocation, lock-free improvement that reduces contention without changing behavior.

Using Interlocked.Increment/Read instead of lock(_statsLock) removes
unnecessary contention on the hot path. Cache hit/miss counters are
simple atomic increments that don't need mutual exclusion with other
state.
Copy link
Copy Markdown
Owner Author

@Sarmkadan Sarmkadan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Straightforward improvement. The Interlocked operations are correct here since hits and misses are independent counters - no need for compound atomicity.

@Sarmkadan Sarmkadan merged commit 0c9691c into main May 21, 2026
Copy link
Copy Markdown
Owner Author

@Sarmkadan Sarmkadan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Straightforward improvement. The Interlocked operations are correct here since hits and misses are independent counters - no need for compound atomicity.

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.

1 participant