Precompute the BitsetCacheKey hashCode, and account for the size of the key #132418
+44
−16
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.
If you trace the
org.elasticsearch.common.cache.Cache
, you can see that it uses thehashCode
of the key object pretty extenstively (see some of the discussion on #96050). For example, in acache.evictEntry(entry)
call, we firsthashCode
to get the right segment, then within the segment wehashCode
to do aget
, then finally we do ahashCode
in theremove
. So that's threehashCode
calls to remove an entry. And the latter two are done while holding thewriteLock
. This isn't the be-all-end-all of performance, but it's a tidy little speedup, and it's easy to write.In the same area, there's a minor bug around the handling of the
NULL_MARKER
in the bitset cache. While theNULL_MARKER
bitset is shared, and so it's fair to count the size of the object as zero, the entries in the cache still take up space because of the size of the keys. Since we're O(segments * queries), it could be the case that many many entries in the cache that could be pointing to theNULL_MARKER
, so let's not consider them to be completely free in terms of memory consumption. (Note: I didn't bother to update the logic around logging a warning if a bitset is created that won't fit in the cache -- so technically we could be off by 24 bytes there. I can change that if you'd prefer we remain technically correct.)As with #132416, I've added @tvernum as a reviewer just as an FYI to him that this PR exists, I don't actually need his +1 specifically (anybody the @elastic/es-security team is fine by me).