CommandEncoder minor optimization

pull/495/head
Nikita 9 years ago
parent 3e95416f34
commit 8dc10d3e5e

@ -15,7 +15,9 @@
*/ */
package org.redisson.client.handler; package org.redisson.client.handler;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.redisson.client.codec.StringCodec; import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.CommandData; import org.redisson.client.protocol.CommandData;
@ -52,6 +54,8 @@ public class CommandEncoder extends MessageToByteEncoder<CommandData<?, ?>> {
private static final char BYTES_PREFIX = '$'; private static final char BYTES_PREFIX = '$';
private static final byte[] CRLF = "\r\n".getBytes(); private static final byte[] CRLF = "\r\n".getBytes();
private static final Map<Long, byte[]> longCache = new HashMap<Long, byte[]>();
@Override @Override
protected void encode(ChannelHandlerContext ctx, CommandData<?, ?> msg, ByteBuf out) throws Exception { protected void encode(ChannelHandlerContext ctx, CommandData<?, ?> msg, ByteBuf out) throws Exception {
out.writeByte(ARGS_PREFIX); out.writeByte(ARGS_PREFIX);
@ -59,7 +63,7 @@ public class CommandEncoder extends MessageToByteEncoder<CommandData<?, ?>> {
if (msg.getCommand().getSubName() != null) { if (msg.getCommand().getSubName() != null) {
len++; len++;
} }
out.writeBytes(toChars(len)); out.writeBytes(convert(len));
out.writeBytes(CRLF); out.writeBytes(CRLF);
writeArgument(out, msg.getCommand().getName().getBytes("UTF-8")); writeArgument(out, msg.getCommand().getName().getBytes("UTF-8"));
@ -127,7 +131,7 @@ public class CommandEncoder extends MessageToByteEncoder<CommandData<?, ?>> {
private void writeArgument(ByteBuf out, byte[] arg) { private void writeArgument(ByteBuf out, byte[] arg) {
out.writeByte(BYTES_PREFIX); out.writeByte(BYTES_PREFIX);
out.writeBytes(toChars(arg.length)); out.writeBytes(convert(arg.length));
out.writeBytes(CRLF); out.writeBytes(CRLF);
out.writeBytes(arg); out.writeBytes(arg);
out.writeBytes(CRLF); out.writeBytes(CRLF);
@ -192,6 +196,13 @@ public class CommandEncoder extends MessageToByteEncoder<CommandData<?, ?>> {
} }
} }
public static byte[] convert(long i) {
if (i >= 0 && i <= 255) {
return longCache.get(i);
}
return toChars(i);
}
public static byte[] toChars(long i) { public static byte[] toChars(long i) {
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
byte[] buf = new byte[size]; byte[] buf = new byte[size];
@ -199,4 +210,12 @@ public class CommandEncoder extends MessageToByteEncoder<CommandData<?, ?>> {
return buf; return buf;
} }
static {
for (long i = 0; i < 256; i++) {
byte[] value = toChars(i);
longCache.put(i, value);
}
}
} }

Loading…
Cancel
Save