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%.
This commit is contained in:
Andrew Steinborn
2019-05-24 06:55:19 -04:00
parent 3cee15a9cb
commit df9883cc6c
3 changed files with 9 additions and 5 deletions

View File

@@ -69,7 +69,11 @@ public class StatusSessionHandler implements MinecraftSessionHandler {
ProxyPingEvent event = new ProxyPingEvent(inboundWrapper, initialPing);
server.getEventManager().fire(event)
.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());
return true;
}

View File

@@ -88,7 +88,7 @@ public enum ProtocolUtils {
* @param buf the buffer to write to
* @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);
writeVarInt(buf, size);
ByteBufUtil.writeUtf8(buf, str);

View File

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