Avoid slice allocation

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-09-04 19:50:09 +02:00
parent d21414c146
commit 885be567fa

View File

@ -237,14 +237,16 @@ public class PlayerSocketConnection extends PlayerConnection {
if (!tickBuffer.canWrite(size)) flush(); if (!tickBuffer.canWrite(size)) flush();
this.tickBuffer.write(buffer.flip()); this.tickBuffer.write(buffer.flip());
} else { } else {
final int positionCache = buffer.position();
final int limitCache = buffer.limit();
final int bufferCount = size / BUFFER_SIZE + 1; final int bufferCount = size / BUFFER_SIZE + 1;
for (int i = 0; i < bufferCount; i++) { for (int i = 0; i < bufferCount; i++) {
ByteBuffer slice = buffer.position(i * BUFFER_SIZE).slice(); buffer.position(i * BUFFER_SIZE);
slice.limit(Math.min(slice.remaining(), BUFFER_SIZE)); buffer.limit(Math.min(size, buffer.position() + BUFFER_SIZE));
if (!tickBuffer.canWrite(slice.remaining())) flush(); if (!tickBuffer.canWrite(buffer.remaining())) flush();
this.tickBuffer.write(slice); this.tickBuffer.write(buffer);
} }
buffer.position(size); buffer.position(positionCache).limit(limitCache);
} }
} }
} }