mirror of
https://github.com/ViaVersion/ViaVersion.git
synced 2024-11-21 17:45:36 +01:00
Don't copy full transformed buf during every transform
The resulting modified buf is the exact same as before
This commit is contained in:
parent
f0eab71644
commit
76db43cab3
@ -351,12 +351,25 @@ public class UserConnectionImpl implements UserConnection {
|
|||||||
throw cancelSupplier.apply(ex);
|
throw cancelSupplier.apply(ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuf transformed = wrapper.allocateOutputBuffer();
|
writeToBuffer(wrapper, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeToBuffer(final PacketWrapperImpl wrapper, final ByteBuf buf) {
|
||||||
|
// Instead of allocating a possible unnecessarily large buffer to write the wrapper contents to,
|
||||||
|
// only allocate the remaining bytes and write the rest to the original buf's head directly.
|
||||||
|
final int remainingBytes = buf.readableBytes();
|
||||||
|
final ByteBuf remainingBuf = buf.alloc().buffer(remainingBytes);
|
||||||
try {
|
try {
|
||||||
wrapper.writeToBuffer(transformed);
|
// Copy before modifying the buffer
|
||||||
buf.clear().writeBytes(transformed);
|
remainingBuf.writeBytes(buf, remainingBytes);
|
||||||
|
|
||||||
|
// Reset indexes, write wrapper contents, then the unread bytes
|
||||||
|
buf.readerIndex(0);
|
||||||
|
buf.writerIndex(0);
|
||||||
|
wrapper.writeProcessedValues(buf);
|
||||||
|
buf.writeBytes(remainingBuf);
|
||||||
} finally {
|
} finally {
|
||||||
transformed.release();
|
remainingBuf.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -222,6 +222,13 @@ public class PacketWrapperImpl implements PacketWrapper {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeToBuffer(ByteBuf buffer) throws InformativeException {
|
public void writeToBuffer(ByteBuf buffer) throws InformativeException {
|
||||||
|
writeProcessedValues(buffer);
|
||||||
|
if (inputBuffer != null) {
|
||||||
|
buffer.writeBytes(inputBuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void writeProcessedValues(ByteBuf buffer) throws InformativeException {
|
||||||
if (id != -1) {
|
if (id != -1) {
|
||||||
Types.VAR_INT.writePrimitive(buffer, id);
|
Types.VAR_INT.writePrimitive(buffer, id);
|
||||||
}
|
}
|
||||||
@ -238,7 +245,6 @@ public class PacketWrapperImpl implements PacketWrapper {
|
|||||||
throw createInformativeException(e, packetValue.type(), i);
|
throw createInformativeException(e, packetValue.type(), i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writeRemaining(buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private InformativeException createInformativeException(final Exception cause, final Type<?> type, final int index) {
|
private InformativeException createInformativeException(final Exception cause, final Type<?> type, final int index) {
|
||||||
@ -264,12 +270,6 @@ public class PacketWrapperImpl implements PacketWrapper {
|
|||||||
packetValues.clear();
|
packetValues.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeRemaining(ByteBuf output) {
|
|
||||||
if (inputBuffer != null) {
|
|
||||||
output.writeBytes(inputBuffer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void send(Class<? extends Protocol> protocol, boolean skipCurrentPipeline) throws InformativeException {
|
public void send(Class<? extends Protocol> protocol, boolean skipCurrentPipeline) throws InformativeException {
|
||||||
send0(protocol, skipCurrentPipeline, true);
|
send0(protocol, skipCurrentPipeline, true);
|
||||||
@ -386,7 +386,7 @@ public class PacketWrapperImpl implements PacketWrapper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ByteBuf allocateOutputBuffer() {
|
private ByteBuf allocateOutputBuffer() {
|
||||||
if (inputBuffer == null) {
|
if (inputBuffer == null) {
|
||||||
return user().getChannel().alloc().buffer();
|
return user().getChannel().alloc().buffer();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user