Skip to content

Commit

Permalink
Enhance SingularArgument caching #575
Browse files Browse the repository at this point in the history
Add cache for negative integer argument values, add cache for known ProtocolKeywords.
  • Loading branch information
mp911de committed Jul 23, 2017
1 parent 7fd4529 commit b0d4706
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
60 changes: 57 additions & 3 deletions src/main/java/com/lambdaworks/redis/protocol/CommandArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ public CommandArgs<K, V> add(byte[] value) {
public CommandArgs<K, V> add(CommandKeyword keyword) {

LettuceAssert.notNull(keyword, "CommandKeyword must not be null");
return add(keyword.bytes);
singularArguments.add(ProtocolKeywordArgument.of(keyword));
return this;
}

/**
Expand All @@ -260,7 +261,8 @@ public CommandArgs<K, V> add(CommandKeyword keyword) {
public CommandArgs<K, V> add(CommandType type) {

LettuceAssert.notNull(type, "CommandType must not be null");
return add(type.bytes);
singularArguments.add(ProtocolKeywordArgument.of(type));
return this;
}

/**
Expand All @@ -272,7 +274,8 @@ public CommandArgs<K, V> add(CommandType type) {
public CommandArgs<K, V> add(ProtocolKeyword keyword) {

LettuceAssert.notNull(keyword, "CommandKeyword must not be null");
return add(keyword.getBytes());
singularArguments.add(ProtocolKeywordArgument.of(keyword));
return this;
}

@Override
Expand Down Expand Up @@ -377,6 +380,50 @@ static void writeBytes(ByteBuf buffer, byte[] value) {
}
}

static class ProtocolKeywordArgument {

static BytesArgument of(ProtocolKeyword protocolKeyword) {

if (protocolKeyword instanceof CommandType) {
return CommandTypeCache.cache[((Enum) protocolKeyword).ordinal()];
}

if (protocolKeyword instanceof CommandKeyword) {
return CommandKeywordCache.cache[((Enum) protocolKeyword).ordinal()];
}

return BytesArgument.of(protocolKeyword.getBytes());
}
}

static class CommandTypeCache {

static final CommandArgs.BytesArgument cache[];

static {

CommandType[] values = CommandType.values();
cache = new BytesArgument[values.length];
for (int i = 0; i < cache.length; i++) {
cache[i] = new BytesArgument(values[i].getBytes());
}
}
}

static class CommandKeywordCache {

static final CommandArgs.BytesArgument cache[];

static {

CommandKeyword[] values = CommandKeyword.values();
cache = new BytesArgument[values.length];
for (int i = 0; i < cache.length; i++) {
cache[i] = new BytesArgument(values[i].getBytes());
}
}
}

static class ByteBufferArgument {

static void writeByteBuffer(ByteBuf target, ByteBuffer value) {
Expand Down Expand Up @@ -416,6 +463,10 @@ static IntegerArgument of(long val) {
return IntegerCache.cache[(int) val];
}

if (val < 0 && -val < IntegerCache.cache.length) {
return IntegerCache.negativeCache[(int) -val];
}

return new IntegerArgument(val);
}

Expand All @@ -442,12 +493,15 @@ static void writeInteger(ByteBuf target, long value) {
static class IntegerCache {

static final IntegerArgument cache[];
static final IntegerArgument negativeCache[];

static {
int high = Integer.getInteger("biz.paluch.redis.CommandArgs.IntegerCache", 128);
cache = new IntegerArgument[high];
negativeCache = new IntegerArgument[high];
for (int i = 0; i < high; i++) {
cache[i] = new IntegerArgument(i);
negativeCache[i] = new IntegerArgument(-i);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public void getFirstIntegerShouldReturnFirstInteger() throws Exception {
assertThat(CommandArgsAccessor.getFirstInteger(args)).isEqualTo(1L);
}

@Test
public void getFirstIntegerShouldReturnFirstNegativeInteger() throws Exception {

CommandArgs<String, String> args = new CommandArgs<>(codec).add(-1L).add(-127).add(-128).add(-129);

assertThat(CommandArgsAccessor.getFirstInteger(args)).isEqualTo(-1L);
}

@Test
public void getFirstStringShouldReturnNull() throws Exception {

Expand Down

0 comments on commit b0d4706

Please sign in to comment.