Skip to content

Commit e1a4e05

Browse files
franz1981vietj
authored andcommitted
Try reuse existing Netty pooled allocator singleton (Fixes #5168)
1 parent 23ca78a commit e1a4e05

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/main/java/io/vertx/core/buffer/impl/VertxByteBufAllocator.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919

2020
public abstract class VertxByteBufAllocator extends AbstractByteBufAllocator {
2121

22+
private static final boolean REUSE_NETTY_ALLOCATOR = Boolean.getBoolean("vertx.reuseNettyAllocators");
23+
2224
/**
23-
* Vert.x pooled allocator.
25+
* Vert.x pooled allocator. It should prefers direct buffers, unless {@link PlatformDependent#hasUnsafe()} is {@code false}.
2426
*/
25-
public static final ByteBufAllocator POOLED_ALLOCATOR = new PooledByteBufAllocator(true);
27+
public static final ByteBufAllocator POOLED_ALLOCATOR = (REUSE_NETTY_ALLOCATOR && PooledByteBufAllocator.defaultPreferDirect()) ?
28+
PooledByteBufAllocator.DEFAULT : new PooledByteBufAllocator(true);
2629
/**
27-
* Vert.x shared unpooled allocator.
30+
* Vert.x shared unpooled allocator. It prefers non-direct buffers.
2831
*/
2932
public static final ByteBufAllocator UNPOOLED_ALLOCATOR = new UnpooledByteBufAllocator(false);
3033

@@ -42,6 +45,10 @@ protected ByteBuf newHeapBuffer(int initialCapacity, int maxCapacity) {
4245
}
4346
};
4447

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

4754
@Override
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2011-2024 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
*/
11+
12+
package io.vertx.core.buffer.impl;
13+
14+
import org.junit.Assert;
15+
import org.junit.Test;
16+
17+
import io.netty.buffer.ByteBufAllocator;
18+
import io.netty.buffer.PooledByteBufAllocator;
19+
20+
public class VertxByteBufAllocatorTest {
21+
22+
23+
@Test
24+
public void defaultShouldNotReuseExistingNettyPooledAllocators() {
25+
Assert.assertNull(System.getProperty("vertx.reuseNettyAllocators"));
26+
Assert.assertNotSame(PooledByteBufAllocator.DEFAULT, VertxByteBufAllocator.POOLED_ALLOCATOR);
27+
Assert.assertNotSame(ByteBufAllocator.DEFAULT, VertxByteBufAllocator.POOLED_ALLOCATOR);
28+
Assert.assertSame(ByteBufAllocator.DEFAULT, PooledByteBufAllocator.DEFAULT);
29+
}
30+
31+
}

0 commit comments

Comments
 (0)