Skip to content

Commit 795e618

Browse files
committed
Add system properties for configuring bytebuf allocator
1 parent 0bec503 commit 795e618

File tree

23 files changed

+51
-32
lines changed

23 files changed

+51
-32
lines changed

common/src/main/java/dev/booky/betterview/common/util/BetterViewUtil.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@
66
import dev.booky.betterview.common.ChunkCacheEntry;
77
import dev.booky.betterview.common.hooks.LevelHook;
88
import io.netty.buffer.ByteBuf;
9+
import io.netty.buffer.ByteBufAllocator;
10+
import io.netty.buffer.PooledByteBufAllocator;
911
import io.netty.buffer.Unpooled;
12+
import io.netty.buffer.UnpooledByteBufAllocator;
1013
import org.jspecify.annotations.NullMarked;
1114

1215
@NullMarked
1316
public final class BetterViewUtil {
1417

18+
private static final boolean ALLOC_UNPOOLED = Boolean.getBoolean("betterview.alloc.unpooled");
19+
private static final boolean ALLOC_HEAP = Boolean.getBoolean("betterview.alloc.heap");
20+
public static final ByteBufAllocator ALLOC = ALLOC_UNPOOLED
21+
? new UnpooledByteBufAllocator(!ALLOC_HEAP)
22+
: new PooledByteBufAllocator(!ALLOC_HEAP);
23+
1524
public static final int MC_MAX_DIMENSION_SIZE = 30_000_000;
1625
public static final int MC_MAX_DIMENSION_SIZE_CHUNKS = MC_MAX_DIMENSION_SIZE << 4;
1726

fabric-1211/src/main/java/dev/booky/betterview/fabric/v1211/mixin/platform/ServerLevelMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private void postInit(CallbackInfo ci) {
160160

161161
public ByteBuf betterview$getEmptyChunkBuf(McChunkPos chunkPos) {
162162
ByteBuf posBuf = BetterViewUtil.encodeChunkPos(chunkPos.getX(), chunkPos.getZ());
163-
return PooledByteBufAllocator.DEFAULT.compositeBuffer(3)
163+
return BetterViewUtil.ALLOC.compositeBuffer(3)
164164
.addComponent(true, PacketUtil.LEVEL_CHUNK_WITH_LIGHT_PACKET_ID_BUF.retainedSlice())
165165
.addComponent(true, posBuf)
166166
.addComponent(true, this.emptyChunkData.retainedSlice());

fabric-1211/src/main/java/dev/booky/betterview/fabric/v1211/mixin/platform/ServerPlayerMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private void onDeathRestore(ServerPlayer from, boolean keepInventory, CallbackIn
9898
public void betterview$sendChunkUnload(int chunkX, int chunkZ) {
9999
ByteBuf packetId = PacketUtil.FORGET_LEVEL_CHUNK_PACKET_ID_BUF.retainedSlice();
100100
ByteBuf chunkPos = BetterViewUtil.encodeChunkPos(McChunkPos.getChunkKey(chunkX, chunkZ));
101-
CompositeByteBuf packetBuf = PooledByteBufAllocator.DEFAULT.compositeBuffer(2)
101+
CompositeByteBuf packetBuf = BetterViewUtil.ALLOC.compositeBuffer(2)
102102
.addComponent(true, packetId)
103103
.addComponent(true, chunkPos);
104104
((PlayerHook) this).getNettyChannel().write(new BypassedPacket(packetBuf));

fabric-1211/src/main/java/dev/booky/betterview/fabric/v1211/packet/ChunkWriter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import ca.spottedleaf.moonrise.patches.starlight.chunk.StarlightChunk;
55
import dev.booky.betterview.common.antixray.AntiXrayProcessor;
6+
import dev.booky.betterview.common.util.BetterViewUtil;
67
import io.netty.buffer.ByteBuf;
78
import io.netty.buffer.PooledByteBufAllocator;
89
import io.netty.buffer.Unpooled;
@@ -74,7 +75,7 @@ public static ByteBuf writeFull(
7475
LevelChunkSection[] sections, byte[][] blockLight, byte @Nullable [][] skyLight
7576
) {
7677
// allocate pooled buffer
77-
ByteBuf buf = PooledByteBufAllocator.DEFAULT.directBuffer();
78+
ByteBuf buf = BetterViewUtil.ALLOC.buffer();
7879
try {
7980
// packet id
8081
buf.writeByte(PacketUtil.LEVEL_CHUNK_WITH_LIGHT_PACKET_ID);
@@ -98,7 +99,7 @@ public static void writeFullBody(
9899
FriendlyByteBuf.writeNbt(buf, heightmapsTag);
99100
// allocate sub-buffer if we're using anti-xray
100101
if (antiXray != null) {
101-
ByteBuf subBuf = PooledByteBufAllocator.DEFAULT.buffer();
102+
ByteBuf subBuf = BetterViewUtil.ALLOC.buffer();
102103
try {
103104
FriendlyByteBuf friendlyBuf = new FriendlyByteBuf(subBuf);
104105
for (int i = 0, len = sections.length; i < len; i++) {

fabric-1213/src/main/java/dev/booky/betterview/fabric/v1213/mixin/platform/ServerLevelMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private void postInit(CallbackInfo ci) {
158158

159159
public ByteBuf betterview$getEmptyChunkBuf(McChunkPos chunkPos) {
160160
ByteBuf posBuf = BetterViewUtil.encodeChunkPos(chunkPos.getX(), chunkPos.getZ());
161-
return PooledByteBufAllocator.DEFAULT.compositeBuffer(3)
161+
return BetterViewUtil.ALLOC.compositeBuffer(3)
162162
.addComponent(true, PacketUtil.LEVEL_CHUNK_WITH_LIGHT_PACKET_ID_BUF.retainedSlice())
163163
.addComponent(true, posBuf)
164164
.addComponent(true, this.emptyChunkData.retainedSlice());

fabric-1213/src/main/java/dev/booky/betterview/fabric/v1213/mixin/platform/ServerPlayerMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private void onDeathRestore(ServerPlayer from, boolean keepInventory, CallbackIn
9898
public void betterview$sendChunkUnload(int chunkX, int chunkZ) {
9999
ByteBuf packetId = PacketUtil.FORGET_LEVEL_CHUNK_PACKET_ID_BUF.retainedSlice();
100100
ByteBuf chunkPos = BetterViewUtil.encodeChunkPos(McChunkPos.getChunkKey(chunkX, chunkZ));
101-
CompositeByteBuf packetBuf = PooledByteBufAllocator.DEFAULT.compositeBuffer(2)
101+
CompositeByteBuf packetBuf = BetterViewUtil.ALLOC.compositeBuffer(2)
102102
.addComponent(true, packetId)
103103
.addComponent(true, chunkPos);
104104
((PlayerHook) this).getNettyChannel().write(new BypassedPacket(packetBuf));

fabric-1213/src/main/java/dev/booky/betterview/fabric/v1213/packet/ChunkWriter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import ca.spottedleaf.moonrise.patches.starlight.chunk.StarlightChunk;
55
import dev.booky.betterview.common.antixray.AntiXrayProcessor;
6+
import dev.booky.betterview.common.util.BetterViewUtil;
67
import io.netty.buffer.ByteBuf;
78
import io.netty.buffer.PooledByteBufAllocator;
89
import io.netty.buffer.Unpooled;
@@ -74,7 +75,7 @@ public static ByteBuf writeFull(
7475
LevelChunkSection[] sections, byte[][] blockLight, byte @Nullable [][] skyLight
7576
) {
7677
// allocate pooled buffer
77-
ByteBuf buf = PooledByteBufAllocator.DEFAULT.directBuffer();
78+
ByteBuf buf = BetterViewUtil.ALLOC.buffer();
7879
try {
7980
// packet id
8081
buf.writeByte(PacketUtil.LEVEL_CHUNK_WITH_LIGHT_PACKET_ID);
@@ -98,7 +99,7 @@ public static void writeFullBody(
9899
FriendlyByteBuf.writeNbt(buf, heightmapsTag);
99100
// allocate sub-buffer if we're using anti-xray
100101
if (antiXray != null) {
101-
ByteBuf subBuf = PooledByteBufAllocator.DEFAULT.buffer();
102+
ByteBuf subBuf = BetterViewUtil.ALLOC.buffer();
102103
try {
103104
FriendlyByteBuf friendlyBuf = new FriendlyByteBuf(subBuf);
104105
for (int i = 0, len = sections.length; i < len; i++) {

fabric-1214/src/main/java/dev/booky/betterview/fabric/v1214/mixin/platform/ServerLevelMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private void postInit(CallbackInfo ci) {
158158

159159
public ByteBuf betterview$getEmptyChunkBuf(McChunkPos chunkPos) {
160160
ByteBuf posBuf = BetterViewUtil.encodeChunkPos(chunkPos.getX(), chunkPos.getZ());
161-
return PooledByteBufAllocator.DEFAULT.compositeBuffer(3)
161+
return BetterViewUtil.ALLOC.compositeBuffer(3)
162162
.addComponent(true, PacketUtil.LEVEL_CHUNK_WITH_LIGHT_PACKET_ID_BUF.retainedSlice())
163163
.addComponent(true, posBuf)
164164
.addComponent(true, this.emptyChunkData.retainedSlice());

fabric-1214/src/main/java/dev/booky/betterview/fabric/v1214/mixin/platform/ServerPlayerMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private void onDeathRestore(ServerPlayer from, boolean keepInventory, CallbackIn
9898
public void betterview$sendChunkUnload(int chunkX, int chunkZ) {
9999
ByteBuf packetId = PacketUtil.FORGET_LEVEL_CHUNK_PACKET_ID_BUF.retainedSlice();
100100
ByteBuf chunkPos = BetterViewUtil.encodeChunkPos(McChunkPos.getChunkKey(chunkX, chunkZ));
101-
CompositeByteBuf packetBuf = PooledByteBufAllocator.DEFAULT.compositeBuffer(2)
101+
CompositeByteBuf packetBuf = BetterViewUtil.ALLOC.compositeBuffer(2)
102102
.addComponent(true, packetId)
103103
.addComponent(true, chunkPos);
104104
((PlayerHook) this).getNettyChannel().write(new BypassedPacket(packetBuf));

fabric-1214/src/main/java/dev/booky/betterview/fabric/v1214/packet/ChunkWriter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import ca.spottedleaf.moonrise.patches.starlight.chunk.StarlightChunk;
55
import dev.booky.betterview.common.antixray.AntiXrayProcessor;
6+
import dev.booky.betterview.common.util.BetterViewUtil;
67
import io.netty.buffer.ByteBuf;
78
import io.netty.buffer.PooledByteBufAllocator;
89
import io.netty.buffer.Unpooled;
@@ -74,7 +75,7 @@ public static ByteBuf writeFull(
7475
LevelChunkSection[] sections, byte[][] blockLight, byte @Nullable [][] skyLight
7576
) {
7677
// allocate pooled buffer
77-
ByteBuf buf = PooledByteBufAllocator.DEFAULT.directBuffer();
78+
ByteBuf buf = BetterViewUtil.ALLOC.buffer();
7879
try {
7980
// packet id
8081
buf.writeByte(PacketUtil.LEVEL_CHUNK_WITH_LIGHT_PACKET_ID);
@@ -98,7 +99,7 @@ public static void writeFullBody(
9899
FriendlyByteBuf.writeNbt(buf, heightmapsTag);
99100
// allocate sub-buffer if we're using anti-xray
100101
if (antiXray != null) {
101-
ByteBuf subBuf = PooledByteBufAllocator.DEFAULT.buffer();
102+
ByteBuf subBuf = BetterViewUtil.ALLOC.buffer();
102103
try {
103104
FriendlyByteBuf friendlyBuf = new FriendlyByteBuf(subBuf);
104105
for (int i = 0, len = sections.length; i < len; i++) {

0 commit comments

Comments
 (0)