Skip to content

Commit adb44bb

Browse files
jpountzbenwtrent
andauthored
Add code comment about use of Collections#synchronizedMap in LRUQueryCache. (#14749)
It may not be totally obvious why this map needs to be synchronized since reads and writes are performed under a read-write lock. Except that unlike most other collections, reads are not thread-safe on a `LinkedHashMap`. Closes #14677 Co-authored-by: Benjamin Trent <[email protected]>
1 parent 2380e0d commit adb44bb

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ public LRUQueryCache(
131131
}
132132
this.skipCacheFactor = skipCacheFactor;
133133

134+
// Note that reads on this LinkedHashMap trigger modifications on the linked list under the
135+
// hood, so reading from multiple threads is not thread-safe. This is why it is wrapped in a
136+
// Collections#synchronizedMap.
134137
uniqueQueries = Collections.synchronizedMap(new LinkedHashMap<>(16, 0.75f, true));
135138
mostRecentlyUsedQueries = uniqueQueries.keySet();
136139
cache = new IdentityHashMap<>();

0 commit comments

Comments
 (0)