Skip to content

Commit ba4c372

Browse files
authored
Merge pull request #5364 from eclipse-vertx/franz1981-4.x_unified_allocator
Franz1981 4.x unified allocator
2 parents a368b77 + da1a68e commit ba4c372

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed

pom.xml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,21 @@
755755
</additionalClasspathElements>
756756
</configuration>
757757
</execution>
758+
<execution>
759+
<id>reuse-netty-allocators</id>
760+
<goals>
761+
<goal>integration-test</goal>
762+
<goal>verify</goal>
763+
</goals>
764+
<configuration>
765+
<includes>
766+
<include>io/vertx/it/ReuseNettyAllocatorsTest.java</include>
767+
</includes>
768+
<systemProperties>
769+
<vertx.reuseNettyAllocators>true</vertx.reuseNettyAllocators>
770+
</systemProperties>
771+
</configuration>
772+
</execution>
758773
</executions>
759774
</plugin>
760775

@@ -986,4 +1001,4 @@
9861001

9871002
</profiles>
9881003

989-
</project>
1004+
</project>

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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
@Test
23+
public void defaultShouldNotReuseExistingNettyPooledAllocators() {
24+
Assert.assertNull(System.getProperty("vertx.reuseNettyAllocators"));
25+
Assert.assertNotSame(PooledByteBufAllocator.DEFAULT, VertxByteBufAllocator.POOLED_ALLOCATOR);
26+
Assert.assertNotSame(ByteBufAllocator.DEFAULT, VertxByteBufAllocator.POOLED_ALLOCATOR);
27+
Assert.assertSame(ByteBufAllocator.DEFAULT, PooledByteBufAllocator.DEFAULT);
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.it;
13+
14+
import io.netty.buffer.ByteBufAllocator;
15+
import io.netty.buffer.PooledByteBufAllocator;
16+
import io.vertx.core.buffer.impl.VertxByteBufAllocator;
17+
import org.junit.Assert;
18+
import org.junit.Test;
19+
20+
public class ReuseNettyAllocatorsTest {
21+
22+
@Test
23+
public void testVertxAllocatorsReuseNettyAllocators() {
24+
Assert.assertEquals("true", System.getProperty("vertx.reuseNettyAllocators"));
25+
Assert.assertSame(PooledByteBufAllocator.DEFAULT, VertxByteBufAllocator.POOLED_ALLOCATOR);
26+
Assert.assertSame(ByteBufAllocator.DEFAULT, VertxByteBufAllocator.POOLED_ALLOCATOR);
27+
Assert.assertSame(ByteBufAllocator.DEFAULT, PooledByteBufAllocator.DEFAULT);
28+
}
29+
}

0 commit comments

Comments
 (0)