forked from hpfxd/PandaSpigot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0038-Optimize-VarInt-reading-and-writing.patch
338 lines (328 loc) · 14.2 KB
/
0038-Optimize-VarInt-reading-and-writing.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: hpfxd <[email protected]>
Date: Sat, 6 Nov 2021 13:04:43 -0400
Subject: [PATCH] Optimize VarInt reading and writing
The VarIntByteDecoder and VarIntUtil classes were borrowed from the Velocity project.
See: https://github.com/VelocityPowered/Velocity
diff --git a/src/main/java/com/hpfxd/pandaspigot/network/VarIntByteDecoder.java b/src/main/java/com/hpfxd/pandaspigot/network/VarIntByteDecoder.java
new file mode 100644
index 0000000000000000000000000000000000000000..df2d56f9a0e38b165d03dbea9e02ecb4d14b1e78
--- /dev/null
+++ b/src/main/java/com/hpfxd/pandaspigot/network/VarIntByteDecoder.java
@@ -0,0 +1,50 @@
+package com.hpfxd.pandaspigot.network;
+
+import io.netty.util.ByteProcessor;
+
+public class VarIntByteDecoder implements ByteProcessor {
+ private int readVarint;
+ private int bytesRead;
+ private DecodeResult result = DecodeResult.TOO_SHORT;
+
+ @Override
+ public boolean process(byte k) {
+ if (k == 0 && bytesRead == 0) {
+ // tentatively say it's invalid, but there's a possibility of redemption
+ result = DecodeResult.RUN_OF_ZEROES;
+ return true;
+ }
+ if (result == DecodeResult.RUN_OF_ZEROES) {
+ return false;
+ }
+ readVarint |= (k & 0x7F) << bytesRead++ * 7;
+ if (bytesRead > 3) {
+ result = DecodeResult.TOO_BIG;
+ return false;
+ }
+ if ((k & 0x80) != 128) {
+ result = DecodeResult.SUCCESS;
+ return false;
+ }
+ return true;
+ }
+
+ public int getReadVarint() {
+ return readVarint;
+ }
+
+ public int getBytesRead() {
+ return bytesRead;
+ }
+
+ public DecodeResult getResult() {
+ return result;
+ }
+
+ public enum DecodeResult {
+ SUCCESS,
+ TOO_SHORT,
+ TOO_BIG,
+ RUN_OF_ZEROES
+ }
+}
diff --git a/src/main/java/com/hpfxd/pandaspigot/network/VarIntUtil.java b/src/main/java/com/hpfxd/pandaspigot/network/VarIntUtil.java
new file mode 100644
index 0000000000000000000000000000000000000000..c0040dd80559d30f963eb952f6ef3372d38f7208
--- /dev/null
+++ b/src/main/java/com/hpfxd/pandaspigot/network/VarIntUtil.java
@@ -0,0 +1,114 @@
+package com.hpfxd.pandaspigot.network;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.handler.codec.CorruptedFrameException;
+
+public class VarIntUtil {
+ private static final int[] VARINT_EXACT_BYTE_LENGTHS = new int[33];
+
+ static {
+ for (int i = 0; i <= 32; ++i) {
+ VARINT_EXACT_BYTE_LENGTHS[i] = (int) Math.ceil((31d - (i - 1)) / 7d);
+ }
+ VARINT_EXACT_BYTE_LENGTHS[32] = 1; // Special case for the number 0.
+ }
+
+ /**
+ * Reads a Minecraft-style VarInt from the specified {@code buf}.
+ *
+ * @param buf the buffer to read from
+ * @return the decoded VarInt
+ */
+ public static int readVarInt(ByteBuf buf) {
+ int read = readVarIntSafely(buf);
+ if (read == Integer.MIN_VALUE) {
+ throw new CorruptedFrameException("Bad VarInt decoded");
+ }
+ return read;
+ }
+
+ /**
+ * Reads a Minecraft-style VarInt from the specified {@code buf}. The difference between this
+ * method and {@link #readVarInt(ByteBuf)} is that this function returns a sentinel value if the
+ * varint is invalid.
+ *
+ * @param buf the buffer to read from
+ * @return the decoded VarInt, or {@code Integer.MIN_VALUE} if the varint is invalid
+ */
+ public static int readVarIntSafely(ByteBuf buf) {
+ int i = 0;
+ int maxRead = Math.min(5, buf.readableBytes());
+ for (int j = 0; j < maxRead; j++) {
+ int k = buf.readByte();
+ i |= (k & 0x7F) << j * 7;
+ if ((k & 0x80) != 128) {
+ return i;
+ }
+ }
+ return Integer.MIN_VALUE;
+ }
+
+ /**
+ * Returns the exact byte size of {@code value} if it were encoded as a VarInt.
+ *
+ * @param value the value to encode
+ * @return the byte size of {@code value} if encoded as a VarInt
+ */
+ public static int varIntBytes(int value) {
+ return VARINT_EXACT_BYTE_LENGTHS[Integer.numberOfLeadingZeros(value)];
+ }
+
+ /**
+ * Writes a Minecraft-style VarInt to the specified {@code buf}.
+ *
+ * @param buf the buffer to read from
+ * @param value the integer to write
+ */
+ public static void writeVarInt(ByteBuf buf, int value) {
+ // Peel the one and two byte count cases explicitly as they are the most common VarInt sizes
+ // that the proxy will write, to improve inlining.
+ if ((value & (0xFFFFFFFF << 7)) == 0) {
+ buf.writeByte(value);
+ } else if ((value & (0xFFFFFFFF << 14)) == 0) {
+ int w = (value & 0x7F | 0x80) << 8 | (value >>> 7);
+ buf.writeShort(w);
+ } else {
+ writeVarIntFull(buf, value);
+ }
+ }
+
+ private static void writeVarIntFull(ByteBuf buf, int value) {
+ // See https://steinborn.me/posts/performance/how-fast-can-you-write-a-varint/
+ if ((value & (0xFFFFFFFF << 7)) == 0) {
+ buf.writeByte(value);
+ } else if ((value & (0xFFFFFFFF << 14)) == 0) {
+ int w = (value & 0x7F | 0x80) << 8 | (value >>> 7);
+ buf.writeShort(w);
+ } else if ((value & (0xFFFFFFFF << 21)) == 0) {
+ int w = (value & 0x7F | 0x80) << 16 | ((value >>> 7) & 0x7F | 0x80) << 8 | (value >>> 14);
+ buf.writeMedium(w);
+ } else if ((value & (0xFFFFFFFF << 28)) == 0) {
+ int w = (value & 0x7F | 0x80) << 24 | (((value >>> 7) & 0x7F | 0x80) << 16)
+ | ((value >>> 14) & 0x7F | 0x80) << 8 | (value >>> 21);
+ buf.writeInt(w);
+ } else {
+ int w = (value & 0x7F | 0x80) << 24 | ((value >>> 7) & 0x7F | 0x80) << 16
+ | ((value >>> 14) & 0x7F | 0x80) << 8 | ((value >>> 21) & 0x7F | 0x80);
+ buf.writeInt(w);
+ buf.writeByte(value >>> 28);
+ }
+ }
+
+ /**
+ * Writes the specified {@code value} as a 21-bit Minecraft VarInt to the specified {@code buf}.
+ * The upper 11 bits will be discarded.
+ *
+ * @param buf the buffer to read from
+ * @param value the integer to write
+ */
+ public static void write21BitVarInt(ByteBuf buf, int value) {
+ // See https://steinborn.me/posts/performance/how-fast-can-you-write-a-varint/
+ int w = (value & 0x7F | 0x80) << 16 | ((value >>> 7) & 0x7F | 0x80) << 8 | (value >>> 14);
+ buf.writeMedium(w);
+ }
+}
diff --git a/src/main/java/net/minecraft/server/PacketPrepender.java b/src/main/java/net/minecraft/server/PacketPrepender.java
index 29d28a45224afbf7f5641a98fee1ae7a416ed6e1..0c0babf7a0c306e1156b8177fc00334999b5adc2 100644
--- a/src/main/java/net/minecraft/server/PacketPrepender.java
+++ b/src/main/java/net/minecraft/server/PacketPrepender.java
@@ -4,26 +4,29 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
[email protected] // PandaSpigot
public class PacketPrepender extends MessageToByteEncoder<ByteBuf> {
- public PacketPrepender() {}
+ public static final PacketPrepender INSTANCE = new PacketPrepender(); // PandaSpigot
+ private PacketPrepender() {} // PandaSpigot - private
protected void a(ChannelHandlerContext channelhandlercontext, ByteBuf bytebuf, ByteBuf bytebuf1) throws Exception {
- int i = bytebuf.readableBytes();
- int j = PacketDataSerializer.a(i);
-
- if (j > 3) {
- throw new IllegalArgumentException("unable to fit " + i + " into " + 3);
- } else {
- PacketDataSerializer packetdataserializer = new PacketDataSerializer(bytebuf1);
-
- packetdataserializer.ensureWritable(j + i);
- packetdataserializer.b(i);
- packetdataserializer.writeBytes(bytebuf, bytebuf.readerIndex(), i);
- }
+ // PandaSpigot start
+ com.hpfxd.pandaspigot.network.VarIntUtil.writeVarInt(bytebuf1, bytebuf.readableBytes());
+ bytebuf1.writeBytes(bytebuf);
+ // PandaSpigot end
}
protected void encode(ChannelHandlerContext channelhandlercontext, ByteBuf object, ByteBuf bytebuf) throws Exception {
this.a(channelhandlercontext, (ByteBuf) object, bytebuf);
}
+ // PandaSpigot start
+ @Override
+ protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect) throws Exception {
+ int anticipatedRequiredCapacity = com.hpfxd.pandaspigot.network.VarIntUtil.varIntBytes(msg.readableBytes())
+ + msg.readableBytes();
+
+ return ctx.alloc().directBuffer(anticipatedRequiredCapacity);
+ }
+ // PandaSpigot end
}
diff --git a/src/main/java/net/minecraft/server/PacketSplitter.java b/src/main/java/net/minecraft/server/PacketSplitter.java
index c51a1f7d1a57acde1e5c7378fe35c30547107052..3f9cdbff6746ed0776b32d9649e5689f397873da 100644
--- a/src/main/java/net/minecraft/server/PacketSplitter.java
+++ b/src/main/java/net/minecraft/server/PacketSplitter.java
@@ -1,5 +1,6 @@
package net.minecraft.server;
+import com.hpfxd.pandaspigot.network.VarIntByteDecoder; // PandaSpigot
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
@@ -12,36 +13,48 @@ public class PacketSplitter extends ByteToMessageDecoder {
public PacketSplitter() {}
protected void decode(ChannelHandlerContext channelhandlercontext, ByteBuf bytebuf, List<Object> list) throws Exception {
- bytebuf.markReaderIndex();
- byte[] abyte = new byte[3];
-
- for (int i = 0; i < abyte.length; ++i) {
- if (!bytebuf.isReadable()) {
- bytebuf.resetReaderIndex();
- return;
+ // PandaSpigot start
+ if (!channelhandlercontext.channel().isActive()) {
+ bytebuf.clear();
+ return;
+ }
+
+ final VarIntByteDecoder reader = new VarIntByteDecoder();
+ int varIntEnd = bytebuf.forEachByte(reader);
+
+ if (varIntEnd == -1) {
+ // We tried to go beyond the end of the buffer. This is probably a good sign that the
+ // buffer was too short to hold a proper varint.
+ if (reader.getResult() == VarIntByteDecoder.DecodeResult.RUN_OF_ZEROES) {
+ // Special case where the entire packet is just a run of zeroes. We ignore them all.
+ bytebuf.clear();
}
-
- abyte[i] = bytebuf.readByte();
- if (abyte[i] >= 0) {
- PacketDataSerializer packetdataserializer = new PacketDataSerializer(Unpooled.wrappedBuffer(abyte));
-
- try {
- int j = packetdataserializer.e();
-
- if (bytebuf.readableBytes() >= j) {
- list.add(bytebuf.readBytes(j));
- return;
- }
-
- bytebuf.resetReaderIndex();
- } finally {
- packetdataserializer.release();
+ return;
+ }
+
+ if (reader.getResult() == VarIntByteDecoder.DecodeResult.RUN_OF_ZEROES) {
+ // this will return to the point where the next varint starts
+ bytebuf.readerIndex(varIntEnd);
+ } else if (reader.getResult() == VarIntByteDecoder.DecodeResult.SUCCESS) {
+ int readVarint = reader.getReadVarint();
+ int bytesRead = reader.getBytesRead();
+ if (readVarint < 0) {
+ bytebuf.clear();
+ throw new CorruptedFrameException("Bad packet length");
+ } else if (readVarint == 0) {
+ // skip over the empty packet(s) and ignore it
+ bytebuf.readerIndex(varIntEnd + 1);
+ } else {
+ int minimumRead = bytesRead + readVarint;
+ if (bytebuf.isReadable(minimumRead)) {
+ list.add(bytebuf.retainedSlice(varIntEnd + 1, readVarint));
+ bytebuf.skipBytes(minimumRead);
}
-
- return;
}
+ } else if (reader.getResult() == VarIntByteDecoder.DecodeResult.TOO_BIG) {
+ bytebuf.clear();
+ throw new CorruptedFrameException("VarInt too big");
}
-
- throw new CorruptedFrameException("length wider than 21-bit");
+ // PandaSpigot end
}
}
diff --git a/src/main/java/net/minecraft/server/ServerConnection.java b/src/main/java/net/minecraft/server/ServerConnection.java
index e390ceef529d98c10bc467ed5517e1ab17b336e0..d36914ec45056328308bb77878a006df4b5e59bd 100644
--- a/src/main/java/net/minecraft/server/ServerConnection.java
+++ b/src/main/java/net/minecraft/server/ServerConnection.java
@@ -115,7 +115,14 @@ public class ServerConnection {
}
if (!disableFlushConsolidation) channel.pipeline().addFirst(new io.netty.handler.flush.FlushConsolidationHandler()); // PandaSpigot
- channel.pipeline().addLast("timeout", new ReadTimeoutHandler(30)).addLast("legacy_query", new LegacyPingHandler(ServerConnection.this)).addLast("splitter", new PacketSplitter()).addLast("decoder", new PacketDecoder(EnumProtocolDirection.SERVERBOUND)).addLast("prepender", new PacketPrepender()).addLast("encoder", new PacketEncoder(EnumProtocolDirection.CLIENTBOUND));
+ // PandaSpigot start - newlines
+ channel.pipeline().addLast("timeout", new ReadTimeoutHandler(30))
+ .addLast("legacy_query", new LegacyPingHandler(ServerConnection.this))
+ .addLast("splitter", new PacketSplitter())
+ .addLast("decoder", new PacketDecoder(EnumProtocolDirection.SERVERBOUND))
+ .addLast("prepender", PacketPrepender.INSTANCE) // PandaSpigot - Share PacketPrepender instance
+ .addLast("encoder", new PacketEncoder(EnumProtocolDirection.CLIENTBOUND));
+ // PandaSpigot end
NetworkManager networkmanager = new NetworkManager(EnumProtocolDirection.SERVERBOUND);
// PandaSpigot start