From 640a22df9efb41e3d29b79916938c1c315be2872 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 14 Dec 2017 18:01:10 +0000 Subject: [PATCH] Remove WeakOrderedQueue from WeakHashMap when FastThreadLocal value was removed if possible. Motivation: We should remove the WeakOrderedQueue from the WeakHashMap directly if possible and only depend on the semantics of the WeakHashMap if there is no other way for us to cleanup it. Modifications: Override onRemoval(...) to remove the WeakOrderedQueue if possible. Result: Less overhead and quicker collection of WeakOrderedQueue for some cases. --- common/src/main/java/io/netty/util/Recycler.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/common/src/main/java/io/netty/util/Recycler.java b/common/src/main/java/io/netty/util/Recycler.java index 75f2e139c8..8bedc37fb6 100644 --- a/common/src/main/java/io/netty/util/Recycler.java +++ b/common/src/main/java/io/netty/util/Recycler.java @@ -114,6 +114,16 @@ public abstract class Recycler { return new Stack(Recycler.this, Thread.currentThread(), maxCapacityPerThread, maxSharedCapacityFactor, ratioMask, maxDelayedQueuesPerThread); } + + @Override + protected void onRemoval(Stack value) { + // Let us remove the WeakOrderQueue from the WeakHashMap directly if its safe to remove some overhead + if (value.threadRef.get() == Thread.currentThread()) { + if (DELAYED_RECYCLED.isSet()) { + DELAYED_RECYCLED.get().remove(value); + } + } + } }; protected Recycler() {