From 88c99f071e18b40264539ae891b1f2962384660f Mon Sep 17 00:00:00 2001 From: md_5 Date: Thu, 11 Apr 2013 20:30:07 +1000 Subject: [PATCH] Reduce ram usage by ~65kb / player. See #229 --- .../java/net/md_5/bungee/netty/CipherCodec.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/proxy/src/main/java/net/md_5/bungee/netty/CipherCodec.java b/proxy/src/main/java/net/md_5/bungee/netty/CipherCodec.java index 0e745f738..18e256237 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/CipherCodec.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/CipherCodec.java @@ -16,8 +16,18 @@ public class CipherCodec extends ByteToByteCodec private Cipher encrypt; private Cipher decrypt; - private byte[] heapIn = new byte[ 0 ]; - private byte[] heapOut = new byte[ 0 ]; + private ThreadLocal heapInLocal = new EmptyByteThreadLocal(); + private ThreadLocal heapOutLocal = new EmptyByteThreadLocal(); + + private static class EmptyByteThreadLocal extends ThreadLocal + { + + @Override + protected byte[] initialValue() + { + return new byte[ 0 ]; + } + } public CipherCodec(Cipher encrypt, Cipher decrypt) { @@ -39,6 +49,7 @@ public class CipherCodec extends ByteToByteCodec private void cipher(ByteBuf in, ByteBuf out, Cipher cipher) throws ShortBufferException { + byte[] heapIn = heapInLocal.get(); int readableBytes = in.readableBytes(); if ( heapIn.length < readableBytes ) { @@ -46,6 +57,7 @@ public class CipherCodec extends ByteToByteCodec } in.readBytes( heapIn, 0, readableBytes ); + byte[] heapOut = heapOutLocal.get(); int outputSize = cipher.getOutputSize( readableBytes ); if ( heapOut.length < outputSize ) {