Avoid useless queue ops in ConcurrentLruCache.clear()

ConcurrentLruCache.clear() previously drained write operations before
cleaning up the cache. This could re-enqueue nodes that clear() was
about to remove, causing useless evictionQueue operations. Now clear()
iterates the cache values directly, removes and marks nodes as removed
first, and drains write operations afterward. Since AddTask fails
silently after a node is marked removed, this avoids the no-op work
and improves performance.

See gh-37287

Closes gh-37292

Signed-off-by: Chengang Guan <guanchengang@qq.com>
This commit is contained in:
guanchengang
2026-09-18 15:53:18 +02:00
committed by GitHub
parent 5d32f6719a
commit b49252ed66
@@ -189,13 +189,13 @@ public final class ConcurrentLruCache<K, V> {
public void clear() {
this.evictionLock.lock();
try {
this.writeOperations.drainAll();
Node<K, V> node;
while ((node = this.evictionQueue.poll()) != null) {
for (Node<K, V> node : this.cache.values()) {
this.cache.remove(node.key, node);
this.evictionQueue.remove(node);
markAsRemoved(node);
}
this.readOperations.clear();
this.writeOperations.drainAll();
}
finally {
this.evictionLock.unlock();