From b09331b79be76b2cf9ff9eff4c6dbfbc9d8ba742 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Tue, 21 May 2019 00:33:35 -0400 Subject: [PATCH] Small optimization for writing packets to the client from the server The layers of indirection to get to the MinecraftConnection causes a small, but not insignificant hit to performance. --- .../backend/BackendPlaySessionHandler.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java index 7515c049d..5128efe9e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java @@ -31,14 +31,15 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { private final VelocityServer server; private final VelocityServerConnection serverConn; private final ClientPlaySessionHandler playerSessionHandler; + private final MinecraftConnection playerConnection; private boolean exceptionTriggered = false; BackendPlaySessionHandler(VelocityServer server, VelocityServerConnection serverConn) { this.server = server; this.serverConn = serverConn; + this.playerConnection = serverConn.getPlayer().getMinecraftConnection(); - MinecraftSessionHandler psh = serverConn.getPlayer().getMinecraftConnection() - .getSessionHandler(); + MinecraftSessionHandler psh = playerConnection.getSessionHandler(); if (!(psh instanceof ClientPlaySessionHandler)) { throw new IllegalStateException( "Initializing BackendPlaySessionHandler with no backing client play session handler!"); @@ -104,7 +105,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { if (PluginMessageUtil.isMcBrand(packet)) { PluginMessage rewritten = PluginMessageUtil.rewriteMinecraftBrand(packet, server.getVersion()); - serverConn.getPlayer().getMinecraftConnection().write(rewritten); + playerConnection.write(rewritten); return true; } @@ -118,15 +119,14 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { return false; } - MinecraftConnection clientConn = serverConn.getPlayer().getMinecraftConnection(); PluginMessageEvent event = new PluginMessageEvent(serverConn, serverConn.getPlayer(), id, packet.getData()); server.getEventManager().fire(event) .thenAcceptAsync(pme -> { - if (pme.getResult().isAllowed() && !clientConn.isClosed()) { - clientConn.write(packet); + if (pme.getResult().isAllowed() && !playerConnection.isClosed()) { + playerConnection.write(packet); } - }, clientConn.eventLoop()); + }, playerConnection.eventLoop()); return true; } @@ -163,12 +163,12 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { @Override public void handleGeneric(MinecraftPacket packet) { - serverConn.getPlayer().getMinecraftConnection().write(packet); + playerConnection.write(packet); } @Override public void handleUnknown(ByteBuf buf) { - serverConn.getPlayer().getMinecraftConnection().write(buf.retain()); + playerConnection.write(buf.retain()); } @Override