3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-12-24 23:30:26 +01:00

Reduce unnecessary memory copies in StatusSessionHandler

Take advantage of the fact that Java has mutable strings when writing
out server ping responses, which Netty can work with when writing out
UTF-8 character sequences. This reduces the memory allocation impact of
server list ping responses by ~31%.
Dieser Commit ist enthalten in:
Andrew Steinborn 2019-05-24 06:55:19 -04:00
Ursprung 3cee15a9cb
Commit df9883cc6c
3 geänderte Dateien mit 9 neuen und 5 gelöschten Zeilen

Datei anzeigen

@ -69,7 +69,11 @@ public class StatusSessionHandler implements MinecraftSessionHandler {
ProxyPingEvent event = new ProxyPingEvent(inboundWrapper, initialPing); ProxyPingEvent event = new ProxyPingEvent(inboundWrapper, initialPing);
server.getEventManager().fire(event) server.getEventManager().fire(event)
.thenRunAsync( .thenRunAsync(
() -> connection.write(new StatusResponse(VelocityServer.GSON.toJson(event.getPing()))), () -> {
StringBuilder json = new StringBuilder();
VelocityServer.GSON.toJson(event.getPing(), json);
connection.write(new StatusResponse(json));
},
connection.eventLoop()); connection.eventLoop());
return true; return true;
} }

Datei anzeigen

@ -88,7 +88,7 @@ public enum ProtocolUtils {
* @param buf the buffer to write to * @param buf the buffer to write to
* @param str the string to write * @param str the string to write
*/ */
public static void writeString(ByteBuf buf, String str) { public static void writeString(ByteBuf buf, CharSequence str) {
int size = ByteBufUtil.utf8Bytes(str); int size = ByteBufUtil.utf8Bytes(str);
writeVarInt(buf, size); writeVarInt(buf, size);
ByteBufUtil.writeUtf8(buf, str); ByteBufUtil.writeUtf8(buf, str);

Datei anzeigen

@ -9,12 +9,12 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public class StatusResponse implements MinecraftPacket { public class StatusResponse implements MinecraftPacket {
private @Nullable String status; private @Nullable CharSequence status;
public StatusResponse() { public StatusResponse() {
} }
public StatusResponse(String status) { public StatusResponse(CharSequence status) {
this.status = status; this.status = status;
} }
@ -22,7 +22,7 @@ public class StatusResponse implements MinecraftPacket {
if (status == null) { if (status == null) {
throw new IllegalStateException("Status is not specified"); throw new IllegalStateException("Status is not specified");
} }
return status; return status.toString();
} }
@Override @Override