From 447d6b3924102cfdc85e618fc4e575ba9c0587b3 Mon Sep 17 00:00:00 2001 From: louxiu Date: Thu, 7 May 2020 16:29:05 +0800 Subject: [PATCH] Use `io.netty.recycler.ratio` directly (#10253) Motivation 1. It's inable to collect all object because RATIO is always >=1 after `safeFindNextPositivePowerOfTwo` 2. Enable drop object in `WeakOrderQueue`(commit: 71860e5b94dbc665fdb8d279d3780d6fe1c618ea) enlarge the drop ratio. We can subtly control the overall drop ratio by using `io.netty.recycler.ratio` directly, Modification - Remove `safeFindNextPositivePowerOfTwo` before set the ratio Results Able to disable drop when recycle object --- .../src/main/java/io/netty/util/Recycler.java | 4 ++-- .../test/java/io/netty/util/RecyclerTest.java | 23 +++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/io/netty/util/Recycler.java b/common/src/main/java/io/netty/util/Recycler.java index 0bbb79f65b..1e002f7d0b 100644 --- a/common/src/main/java/io/netty/util/Recycler.java +++ b/common/src/main/java/io/netty/util/Recycler.java @@ -82,7 +82,7 @@ public abstract class Recycler { // By default we allow one push to a Recycler for each 8th try on handles that were never recycled before. // This should help to slowly increase the capacity of the recycler while not be too sensitive to allocation // bursts. - RATIO = safeFindNextPositivePowerOfTwo(SystemPropertyUtil.getInt("io.netty.recycler.ratio", 8)); + RATIO = max(0, SystemPropertyUtil.getInt("io.netty.recycler.ratio", 8)); if (logger.isDebugEnabled()) { if (DEFAULT_MAX_CAPACITY_PER_THREAD == 0) { @@ -138,7 +138,7 @@ public abstract class Recycler { protected Recycler(int maxCapacityPerThread, int maxSharedCapacityFactor, int ratio, int maxDelayedQueuesPerThread) { - interval = safeFindNextPositivePowerOfTwo(ratio); + interval = max(0, ratio); if (maxCapacityPerThread <= 0) { this.maxCapacityPerThread = 0; this.maxSharedCapacityFactor = 1; diff --git a/common/src/test/java/io/netty/util/RecyclerTest.java b/common/src/test/java/io/netty/util/RecyclerTest.java index df5366a3e3..b226c84fac 100644 --- a/common/src/test/java/io/netty/util/RecyclerTest.java +++ b/common/src/test/java/io/netty/util/RecyclerTest.java @@ -26,8 +26,14 @@ import static org.junit.Assert.*; public class RecyclerTest { - private static Recycler newRecycler(int max) { - return new Recycler(max) { + private static Recycler newRecycler(int maxCapacityPerThread) { + return newRecycler(maxCapacityPerThread, 2, 8, 2); + } + + private static Recycler newRecycler(int maxCapacityPerThread, int maxSharedCapacityFactor, + int ratio, int maxDelayedQueuesPerThread) { + return new Recycler(maxCapacityPerThread, maxSharedCapacityFactor, ratio, + maxDelayedQueuesPerThread) { @Override protected HandledObject newObject( Recycler.Handle handle) { @@ -122,6 +128,19 @@ public class RecyclerTest { object2.recycle(); } + @Test + public void testRecycleDisableDrop() { + Recycler recycler = newRecycler(1024, 2, 0, 2); + HandledObject object = recycler.get(); + object.recycle(); + HandledObject object2 = recycler.get(); + assertSame(object, object2); + object2.recycle(); + HandledObject object3 = recycler.get(); + assertSame(object, object3); + object3.recycle(); + } + /** * Test to make sure bug #2848 never happens again * https://github.com/netty/netty/issues/2848