From f77891cc1786806630f6c4408d5d37abb1891e7b Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Wed, 29 Aug 2018 08:21:07 +0200 Subject: [PATCH] We should prefer direct buffers if we can access the cleaner even if sun.misc.Unsafe is not present. (#8233) Motivation: We should prefer direct buffers whenever we can use the cleaner even if sun.misc.Unsafe is not present. Modifications: Correctly prefer direct buffers in all cases. Result: More correct code. --- .../io/netty/util/internal/PlatformDependent.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/io/netty/util/internal/PlatformDependent.java b/common/src/main/java/io/netty/util/internal/PlatformDependent.java index d7a8a26218..6a534bcff0 100644 --- a/common/src/main/java/io/netty/util/internal/PlatformDependent.java +++ b/common/src/main/java/io/netty/util/internal/PlatformDependent.java @@ -79,8 +79,7 @@ public final class PlatformDependent { private static final boolean CAN_ENABLE_TCP_NODELAY_BY_DEFAULT = !isAndroid(); private static final Throwable UNSAFE_UNAVAILABILITY_CAUSE = unsafeUnavailabilityCause0(); - private static final boolean DIRECT_BUFFER_PREFERRED = - UNSAFE_UNAVAILABILITY_CAUSE == null && !SystemPropertyUtil.getBoolean("io.netty.noPreferDirect", false); + private static final boolean DIRECT_BUFFER_PREFERRED; private static final long MAX_DIRECT_MEMORY = maxDirectMemory0(); private static final int MPSC_CHUNK_SIZE = 1024; @@ -128,9 +127,6 @@ public final class PlatformDependent { } }; } - if (logger.isDebugEnabled()) { - logger.debug("-Dio.netty.noPreferDirect: {}", !DIRECT_BUFFER_PREFERRED); - } /* * We do not want to log this message if unsafe is explicitly disabled. Do not remove the explicit no unsafe @@ -190,6 +186,13 @@ public final class PlatformDependent { } else { CLEANER = NOOP; } + + // We should always prefer direct buffers by default if we can use a Cleaner to release direct buffers. + DIRECT_BUFFER_PREFERRED = CLEANER != NOOP + && !SystemPropertyUtil.getBoolean("io.netty.noPreferDirect", false); + if (logger.isDebugEnabled()) { + logger.debug("-Dio.netty.noPreferDirect: {}", !DIRECT_BUFFER_PREFERRED); + } } public static boolean hasDirectBufferNoCleanerConstructor() {