Avoid unnecessary synchronization in QueryCache#queryContains - #2298
Open
1wairesd wants to merge 2 commits into
Open
Avoid unnecessary synchronization in QueryCache#queryContains#22981wairesd wants to merge 2 commits into
1wairesd wants to merge 2 commits into
Conversation
Use a lock-free ConcurrentHashMap.get() before falling back to compute() to avoid bucket-level locking when the result is already cached. On the hot path (repeated queries to the same block position within a tick), this eliminates contention on the CHM segment lock entirely, reducing overhead to a single volatile read.
|
It looks like the lock ensures that access to the inner map is synchronized, which seems to be required. |
Replace EnumMap with ConcurrentHashMap in QueryOption.createCache() so that concurrent reads and computeIfAbsent calls on the inner map are thread-safe. This allows QueryCache.queryContains() to use a lock-free get() fast-path before falling back to compute(), avoiding unnecessary bucket-level locking on repeated queries to the same location. Use QueryOption.values().length as initial capacity since the map will never hold more entries than there are QueryOption enum constants.
Author
|
Fixed the thread-safety concern by replacing EnumMap with ConcurrentHashMap for the inner option map in all three QueryOption.createCache() implementations. The computeIfAbsent calls on the inner map are now safe, and the lock-free get() fast-path in QueryCache.queryContains() is correct. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Use a lock-free ConcurrentHashMap.get() before falling back to compute() to avoid bucket-level locking when the result is already cached. On the hot path (repeated queries to the same block position within a tick), this eliminates contention on the CHM segment lock entirely, reducing overhead to a single volatile read.