From df9883cc6c24f93784f5bcb7cff92c2bae3e03ec Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 24 May 2019 06:55:19 -0400 Subject: [PATCH] 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%. --- .../proxy/connection/client/StatusSessionHandler.java | 6 +++++- .../com/velocitypowered/proxy/protocol/ProtocolUtils.java | 2 +- .../proxy/protocol/packet/StatusResponse.java | 6 +++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/StatusSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/StatusSessionHandler.java index 4f38ffb44..0916d4aea 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/StatusSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/StatusSessionHandler.java @@ -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; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java index 6b50392da..907a8640f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java @@ -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); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusResponse.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusResponse.java index 84493513d..df0d856cc 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusResponse.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusResponse.java @@ -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