Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try reuse existing Netty pooled allocator singleton (Fixes #5168) #5292

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/main/java/io/vertx/core/buffer/impl/VertxByteBufAllocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@

public abstract class VertxByteBufAllocator extends AbstractByteBufAllocator {

private static final boolean REUSE_NETTY_ALLOCATOR = Boolean.getBoolean("vertx.reuseNettyAllocators");

/**
* Vert.x pooled allocator.
* Vert.x pooled allocator. It should prefers direct buffers, unless {@link PlatformDependent#hasUnsafe()} is {@code false}.
*/
public static final ByteBufAllocator POOLED_ALLOCATOR = new PooledByteBufAllocator(true);
public static final ByteBufAllocator POOLED_ALLOCATOR = (REUSE_NETTY_ALLOCATOR && PooledByteBufAllocator.defaultPreferDirect()) ?
PooledByteBufAllocator.DEFAULT : new PooledByteBufAllocator(true);
/**
* Vert.x shared unpooled allocator.
* Vert.x shared unpooled allocator. It prefers non-direct buffers.
*/
public static final ByteBufAllocator UNPOOLED_ALLOCATOR = new UnpooledByteBufAllocator(false);

Expand All @@ -42,6 +45,10 @@ protected ByteBuf newHeapBuffer(int initialCapacity, int maxCapacity) {
}
};

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

public class VertxByteBufAllocatorTest {


@Test
public void defaultShouldNotReuseExistingNettyPooledAllocators() {
Assert.assertNull(System.getProperty("vertx.reuseNettyAllocators"));
Assert.assertNotSame(PooledByteBufAllocator.DEFAULT, VertxByteBufAllocator.POOLED_ALLOCATOR);
Assert.assertNotSame(ByteBufAllocator.DEFAULT, VertxByteBufAllocator.POOLED_ALLOCATOR);
Assert.assertSame(ByteBufAllocator.DEFAULT, PooledByteBufAllocator.DEFAULT);
}

}
Loading