diff --git a/leaf-server/minecraft-patches/features/0127-Bulk-writes-to-writeLongArray-during-chunk-loading.patch b/leaf-server/minecraft-patches/features/0127-Bulk-writes-to-writeLongArray-during-chunk-loading.patch index 947081e1..8ac54e00 100644 --- a/leaf-server/minecraft-patches/features/0127-Bulk-writes-to-writeLongArray-during-chunk-loading.patch +++ b/leaf-server/minecraft-patches/features/0127-Bulk-writes-to-writeLongArray-during-chunk-loading.patch @@ -5,10 +5,10 @@ Subject: [PATCH] Bulk writes to writeLongArray during chunk loading diff --git a/net/minecraft/network/FriendlyByteBuf.java b/net/minecraft/network/FriendlyByteBuf.java -index abb0141426fd716e79a947b9498a8351daa342fc..5353b8331cf9c428ab7ffba6ebeac4fa323af456 100644 +index abb0141426fd716e79a947b9498a8351daa342fc..6971f93c3f6008f2c2f99fc52e2a3058fd8b7659 100644 --- a/net/minecraft/network/FriendlyByteBuf.java +++ b/net/minecraft/network/FriendlyByteBuf.java -@@ -341,10 +341,33 @@ public class FriendlyByteBuf extends ByteBuf { +@@ -341,10 +341,43 @@ public class FriendlyByteBuf extends ByteBuf { public FriendlyByteBuf writeLongArray(long[] array) { this.writeVarInt(array.length); @@ -16,29 +16,39 @@ index abb0141426fd716e79a947b9498a8351daa342fc..5353b8331cf9c428ab7ffba6ebeac4fa + if (array.length == 0) { + return this; + } ++ int neededBytes = array.length * Long.BYTES; ++ int maxWritableBytes = this.source.maxWritableBytes(); + -+ this.source.ensureWritable(array.length * Long.BYTES); -+ int writerIndex = this.source.writerIndex(); ++ if (maxWritableBytes >= neededBytes) { ++ this.source.ensureWritable(neededBytes); ++ int writerIndex = this.source.writerIndex(); + -+ if (this.source.hasArray()) { -+ byte[] dest = this.source.array(); -+ int offset = this.source.arrayOffset() + writerIndex; ++ if (this.source.hasArray()) { ++ byte[] dest = this.source.array(); ++ int offset = this.source.arrayOffset() + writerIndex; ++ ++ ByteBuffer buf = ByteBuffer.wrap(dest, offset, neededBytes).order(this.source.order()); ++ buf.asLongBuffer().put(array); - for (long l : array) { - this.writeLong(l); -+ ByteBuffer buf = ByteBuffer.wrap(dest, offset, array.length * Long.BYTES).order(this.source.order()); -+ buf.asLongBuffer().put(array); -+ -+ this.source.writerIndex(writerIndex + array.length * Long.BYTES); -+ } else if (this.source.nioBufferCount() > 0) { -+ ByteBuffer nioBuf = this.source.nioBuffer(writerIndex, array.length * Long.BYTES); -+ nioBuf.asLongBuffer().put(array); -+ this.source.writerIndex(writerIndex + array.length * Long.BYTES); ++ this.source.writerIndex(writerIndex + neededBytes); ++ } else if (this.source.nioBufferCount() > 0) { ++ ByteBuffer nioBuf = this.source.nioBuffer(writerIndex, neededBytes); ++ nioBuf.asLongBuffer().put(array); ++ this.source.writerIndex(writerIndex + neededBytes); ++ } else { ++ ByteBuffer temp = ByteBuffer.allocate(neededBytes).order(this.source.order()); ++ temp.asLongBuffer().put(array); ++ temp.rewind(); ++ this.source.writeBytes(temp); ++ } + } else { -+ ByteBuffer temp = ByteBuffer.allocate(array.length * Long.BYTES).order(this.source.order()); -+ temp.asLongBuffer().put(array); -+ temp.rewind(); -+ this.source.writeBytes(temp); ++ // Not enough space even at max capacity, use traditional approach ++ // which will write each element individually (and handle growing the buffer as needed) ++ for (long l : array) { ++ this.writeLong(l); ++ } } + // Leaf end - Bulk writes to writeLongArray during chunk loading