3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-17 05:20:14 +01:00

Improve writeVarInt inlining by peeling the two most common cases

Dieser Commit ist enthalten in:
Andrew Steinborn 2021-05-09 02:57:01 -04:00
Ursprung 150fd9a9cf
Commit 5170da8fd7

Datei anzeigen

@ -120,6 +120,19 @@ public enum ProtocolUtils {
* @param value the integer to write * @param value the integer to write
*/ */
public static void writeVarInt(ByteBuf buf, int value) { 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/ // See https://steinborn.me/posts/performance/how-fast-can-you-write-a-varint/
if ((value & (0xFFFFFFFF << 7)) == 0) { if ((value & (0xFFFFFFFF << 7)) == 0) {
buf.writeByte(value); buf.writeByte(value);
@ -135,7 +148,7 @@ public enum ProtocolUtils {
buf.writeInt(w); buf.writeInt(w);
} else { } else {
int w = (value & 0x7F | 0x80) << 24 | ((value >>> 7) & 0x7F | 0x80) << 16 int w = (value & 0x7F | 0x80) << 24 | ((value >>> 7) & 0x7F | 0x80) << 16
| ((value >>> 14) & 0x7F | 0x80) << 8 | ((value >>> 21) & 0x7F | 0x80); | ((value >>> 14) & 0x7F | 0x80) | ((value >>> 21) & 0x7F | 0x80);
buf.writeInt(w); buf.writeInt(w);
buf.writeByte(value >>> 28); buf.writeByte(value >>> 28);
} }