From 18058bc3c8d89c7b67ac9d0ac953183abd361b1b Mon Sep 17 00:00:00 2001 From: TheMode Date: Mon, 9 Aug 2021 23:54:45 +0200 Subject: [PATCH] Reduce ByteBuffer allocation --- .../server/network/player/PlayerSocketConnection.java | 5 ++--- .../java/net/minestom/server/utils/binary/BinaryBuffer.java | 2 +- 2 files changed, 3 insertions(+), 4 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 b583ddf01..d958242be 100644 --- a/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java +++ b/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java @@ -228,12 +228,11 @@ public class PlayerSocketConnection extends PlayerConnection { public void write(@NotNull ByteBuffer buffer) { synchronized (tickBuffer) { - buffer.flip(); - if (!tickBuffer.canWrite(buffer.limit())) { + if (!tickBuffer.canWrite(buffer.position())) { // Tick buffer is full, flush before appending flush(); } - this.tickBuffer.write(buffer); + this.tickBuffer.write(buffer.flip()); } } diff --git a/src/main/java/net/minestom/server/utils/binary/BinaryBuffer.java b/src/main/java/net/minestom/server/utils/binary/BinaryBuffer.java index b4c4b5e0e..d0a60d40e 100644 --- a/src/main/java/net/minestom/server/utils/binary/BinaryBuffer.java +++ b/src/main/java/net/minestom/server/utils/binary/BinaryBuffer.java @@ -136,7 +136,7 @@ public final class BinaryBuffer { } public void readChannel(ReadableByteChannel channel) throws IOException { - final int count = channel.read(asByteBuffer(readerOffset, capacity)); + final int count = channel.read(nioBuffer.position(readerOffset)); if (count == -1) { // EOS throw new IOException("Disconnected");