diff --git a/src/main/java/io/vertx/core/buffer/impl/VertxByteBufAllocator.java b/src/main/java/io/vertx/core/buffer/impl/VertxByteBufAllocator.java index 48f02daf72d..0d3c31d18cf 100644 --- a/src/main/java/io/vertx/core/buffer/impl/VertxByteBufAllocator.java +++ b/src/main/java/io/vertx/core/buffer/impl/VertxByteBufAllocator.java @@ -22,11 +22,13 @@ public abstract class VertxByteBufAllocator extends AbstractByteBufAllocator { /** * Vert.x pooled allocator. */ - public static final ByteBufAllocator POOLED_ALLOCATOR = new PooledByteBufAllocator(true); + public static final ByteBufAllocator POOLED_ALLOCATOR = PooledByteBufAllocator.defaultPreferDirect() ? + PooledByteBufAllocator.DEFAULT : new PooledByteBufAllocator(true); /** * Vert.x shared unpooled allocator. */ - public static final ByteBufAllocator UNPOOLED_ALLOCATOR = new UnpooledByteBufAllocator(false); + public static final ByteBufAllocator UNPOOLED_ALLOCATOR = PooledByteBufAllocator.defaultPreferDirect() ? + UnpooledByteBufAllocator.DEFAULT : new UnpooledByteBufAllocator(false); private static final VertxByteBufAllocator UNSAFE_IMPL = new VertxByteBufAllocator() { @Override @@ -42,6 +44,10 @@ protected ByteBuf newHeapBuffer(int initialCapacity, int maxCapacity) { } }; + /** + * Vert.x shared unpooled allocator.
+ * Differently from {@link #POOLED_ALLOCATOR}, its heap buffers are not reference counted. + */ public static final VertxByteBufAllocator DEFAULT = PlatformDependent.hasUnsafe() ? UNSAFE_IMPL : IMPL; @Override diff --git a/src/main/java/io/vertx/core/net/impl/TCPServerBase.java b/src/main/java/io/vertx/core/net/impl/TCPServerBase.java index 08cac0c2773..3dfd22f573b 100644 --- a/src/main/java/io/vertx/core/net/impl/TCPServerBase.java +++ b/src/main/java/io/vertx/core/net/impl/TCPServerBase.java @@ -26,6 +26,7 @@ import io.vertx.core.Handler; import io.vertx.core.Promise; import io.vertx.core.buffer.impl.PartialPooledByteBufAllocator; +import io.vertx.core.buffer.impl.VertxByteBufAllocator; import io.vertx.core.impl.ContextInternal; import io.vertx.core.impl.future.PromiseInternal; import io.vertx.core.impl.VertxInternal; @@ -245,7 +246,7 @@ private synchronized Future listen(SocketAddress localAddress, ContextI if (options.isSsl()) { bootstrap.childOption(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE); } else { - bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); + bootstrap.childOption(ChannelOption.ALLOCATOR, VertxByteBufAllocator.POOLED_ALLOCATOR); } bootstrap.childHandler(channelBalancer); diff --git a/src/test/java/io/vertx/core/buffer/impl/VertxByteBufAllocatorTest.java b/src/test/java/io/vertx/core/buffer/impl/VertxByteBufAllocatorTest.java new file mode 100644 index 00000000000..dace95f133d --- /dev/null +++ b/src/test/java/io/vertx/core/buffer/impl/VertxByteBufAllocatorTest.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2011-2024 Contributors to the Eclipse Foundation + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 + * which is available at https://www.apache.org/licenses/LICENSE-2.0. + * + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 + */ + +package io.vertx.core.buffer.impl; + +import org.junit.Assert; +import org.junit.Test; + +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.PooledByteBufAllocator; +import io.netty.buffer.UnpooledByteBufAllocator; + +public class VertxByteBufAllocatorTest { + + @Test + public void shouldReuseTheExistingNettyAllocators() { + Assert.assertTrue(PooledByteBufAllocator.defaultPreferDirect()); + Assert.assertSame(PooledByteBufAllocator.DEFAULT, VertxByteBufAllocator.POOLED_ALLOCATOR); + Assert.assertSame(ByteBufAllocator.DEFAULT, VertxByteBufAllocator.POOLED_ALLOCATOR); + Assert.assertSame(UnpooledByteBufAllocator.DEFAULT, VertxByteBufAllocator.UNPOOLED_ALLOCATOR); + } + +}