From 3d5ae6fe942f4d7e0b83a14548b9ef32aacab87c Mon Sep 17 00:00:00 2001 From: TheMode Date: Fri, 3 Sep 2021 13:54:38 +0200 Subject: [PATCH] Pool main connection buffer Signed-off-by: TheMode --- .../network/player/PlayerSocketConnection.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java b/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java index 84e09b34a..ffe814fae 100644 --- a/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java +++ b/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java @@ -75,7 +75,7 @@ public class PlayerSocketConnection extends PlayerConnection { private final Object bufferLock = new Object(); private final List waitingBuffers = new ArrayList<>(); - private BinaryBuffer tickBuffer = BinaryBuffer.ofSize(BUFFER_SIZE); + private BinaryBuffer tickBuffer = getPooledBuffer(); private volatile BinaryBuffer cacheBuffer; public PlayerSocketConnection(@NotNull Worker worker, @NotNull SocketChannel channel, SocketAddress remoteAddress) { @@ -289,10 +289,7 @@ public class PlayerSocketConnection extends PlayerConnection { } } // Update tick buffer - BinaryBuffer newBuffer = POOLED_BUFFERS.poll(); - if (newBuffer == null) newBuffer = BinaryBuffer.ofSize(BUFFER_SIZE); - newBuffer.clear(); - this.tickBuffer = newBuffer; + this.tickBuffer = getPooledBuffer(); } } @@ -453,4 +450,14 @@ public class PlayerSocketConnection extends PlayerConnection { public void setNonce(byte[] nonce) { this.nonce = nonce; } + + private static BinaryBuffer getPooledBuffer() { + BinaryBuffer newBuffer = POOLED_BUFFERS.poll(); + if (newBuffer == null) { + newBuffer = BinaryBuffer.ofSize(BUFFER_SIZE); + } else { + newBuffer.clear(); + } + return newBuffer; + } }