Skip to content

Commit

Permalink
Try reuse existing Netty pooled allocator singleton (Fixes #5168)
Browse files Browse the repository at this point in the history
  • Loading branch information
franz1981 committed Sep 1, 2024
1 parent 83c72a4 commit ca6c707
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,6 +44,10 @@ protected ByteBuf newHeapBuffer(int initialCapacity, int maxCapacity) {
}
};

/**
* Vert.x shared unpooled allocator.<br>
* Differently from {@link #POOLED_ALLOCATOR}, its heap buffers are not reference counted.
*/
public static final VertxByteBufAllocator DEFAULT = PlatformDependent.hasUnsafe() ? UNSAFE_IMPL : IMPL;

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/vertx/core/net/impl/TCPServerBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -245,7 +246,7 @@ private synchronized Future<Channel> 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);
Expand Down

0 comments on commit ca6c707

Please sign in to comment.