Skip to content

Commit 6185ebd

Browse files
committed
Polishing #2610
Reduce code duplications. Add exact optimization to ASCII StringCodec. Tweak Javadoc. Original pull request: #2768
1 parent 68c89ae commit 6185ebd

File tree

3 files changed

+57
-54
lines changed

3 files changed

+57
-54
lines changed

src/main/java/io/lettuce/core/codec/StringCodec.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717

1818
import java.nio.ByteBuffer;
1919
import java.nio.CharBuffer;
20-
import java.nio.charset.*;
20+
import java.nio.charset.CharacterCodingException;
21+
import java.nio.charset.Charset;
22+
import java.nio.charset.CharsetEncoder;
23+
import java.nio.charset.CoderResult;
24+
import java.nio.charset.StandardCharsets;
2125

2226
import io.lettuce.core.internal.LettuceAssert;
2327
import io.netty.buffer.ByteBuf;
@@ -100,6 +104,16 @@ public int estimateSize(Object keyOrValue) {
100104
return 0;
101105
}
102106

107+
@Override
108+
public boolean isEstimateExact() {
109+
110+
if (ascii) {
111+
return true;
112+
}
113+
114+
return ToByteBufEncoder.super.isEstimateExact();
115+
}
116+
103117
@Override
104118
public void encodeValue(String value, ByteBuf target) {
105119
encode(value, target);
@@ -186,8 +200,8 @@ public void encode(String str, ByteBuf target) {
186200
}
187201

188202
/**
189-
* Calculate either the maximum number of bytes a string may occupy in a given character set or
190-
* the average number of bytes it may hold.
203+
* Calculate either the maximum number of bytes a string may occupy in a given character set or the average number of bytes
204+
* it may hold.
191205
*/
192206
int sizeOf(String value, boolean estimate) {
193207

@@ -205,4 +219,5 @@ int sizeOf(String value, boolean estimate) {
205219

206220
return (int) maxBytesPerChar * value.length();
207221
}
222+
208223
}

src/main/java/io/lettuce/core/codec/ToByteBufEncoder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ public interface ToByteBufEncoder<K, V> {
5858
int estimateSize(Object keyOrValue);
5959

6060
/**
61-
* Returns true if {@link ToByteBufEncoder#estimateSize(Object)} returns exact size
62-
* This is used as an optimisation to reduce memory allocations when encoding data
61+
* Returns {@code true} if {@link #estimateSize(Object)} returns exact size This is used as an optimization to reduce memory
62+
* allocations when encoding data.
6363
*
64-
* @return true if {@link ToByteBufEncoder#estimateSize(Object)} returns exact size
64+
* @return {@code true} if {@link #estimateSize(Object)} returns exact size.
65+
* @since 6.3.2
6566
*/
6667
default boolean isEstimateExact() {
6768
return false;

src/main/java/io/lettuce/core/protocol/CommandArgs.java

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -547,9 +547,9 @@ static void writeInteger(ByteBuf target, long value) {
547547

548548
static class IntegerCache {
549549

550-
static final IntegerArgument cache[];
550+
static final IntegerArgument[] cache;
551551

552-
static final IntegerArgument negativeCache[];
552+
static final IntegerArgument[] negativeCache;
553553

554554
static {
555555
int high = Integer.getInteger("io.lettuce.core.CommandArgs.IntegerCache", 128);
@@ -648,8 +648,8 @@ static void writeString(ByteBuf target, char[] value) {
648648
IntegerArgument.writeInteger(target, value.length);
649649
target.writeBytes(CRLF);
650650

651-
for (int i = 0; i < value.length; i++) {
652-
target.writeByte((byte) value[i]);
651+
for (char c : value) {
652+
target.writeByte((byte) c);
653653
}
654654
target.writeBytes(CRLF);
655655
}
@@ -681,29 +681,7 @@ static <K, V> KeyArgument<K, V> of(K key, RedisCodec<K, V> codec) {
681681
void encode(ByteBuf target) {
682682

683683
if (codec instanceof ToByteBufEncoder) {
684-
685-
ToByteBufEncoder<K, V> toByteBufEncoder = (ToByteBufEncoder<K, V>) codec;
686-
687-
if (toByteBufEncoder.isEstimateExact()) {
688-
target.writeByte('$');
689-
690-
IntegerArgument.writeInteger(target, toByteBufEncoder.estimateSize(key));
691-
target.writeBytes(CRLF);
692-
693-
toByteBufEncoder.encodeKey(key, target);
694-
target.writeBytes(CRLF);
695-
} else {
696-
ByteBuf temporaryBuffer = target.alloc().buffer(toByteBufEncoder.estimateSize(key) + 6);
697-
698-
try {
699-
700-
toByteBufEncoder.encodeKey(key, temporaryBuffer);
701-
ByteBufferArgument.writeByteBuf(target, temporaryBuffer);
702-
} finally {
703-
temporaryBuffer.release();
704-
}
705-
}
706-
684+
CommandArgs.encode(target, (ToByteBufEncoder<K, K>) codec, key, ToByteBufEncoder::encodeKey);
707685
return;
708686
}
709687

@@ -737,27 +715,7 @@ static <K, V> ValueArgument<K, V> of(V val, RedisCodec<K, V> codec) {
737715
void encode(ByteBuf target) {
738716

739717
if (codec instanceof ToByteBufEncoder) {
740-
741-
ToByteBufEncoder<K, V> toByteBufEncoder = (ToByteBufEncoder<K, V>) codec;
742-
if (toByteBufEncoder.isEstimateExact()) {
743-
target.writeByte('$');
744-
745-
IntegerArgument.writeInteger(target, toByteBufEncoder.estimateSize(val));
746-
target.writeBytes(CRLF);
747-
748-
toByteBufEncoder.encodeValue(val, target);
749-
target.writeBytes(CRLF);
750-
} else {
751-
ByteBuf temporaryBuffer = target.alloc().buffer(toByteBufEncoder.estimateSize(val) + 6);
752-
753-
try {
754-
toByteBufEncoder.encodeValue(val, temporaryBuffer);
755-
ByteBufferArgument.writeByteBuf(target, temporaryBuffer);
756-
} finally {
757-
temporaryBuffer.release();
758-
}
759-
}
760-
718+
CommandArgs.encode(target, (ToByteBufEncoder<V, V>) codec, val, ToByteBufEncoder::encodeValue);
761719
return;
762720
}
763721

@@ -771,4 +729,33 @@ public String toString() {
771729

772730
}
773731

732+
static <T> void encode(ByteBuf target, ToByteBufEncoder<T, T> encoder, T item, EncodeFunction<T> encodeFunction) {
733+
734+
if (encoder.isEstimateExact()) {
735+
736+
target.writeByte('$');
737+
IntegerArgument.writeInteger(target, encoder.estimateSize(item));
738+
target.writeBytes(CRLF);
739+
740+
encodeFunction.encode(encoder, item, target);
741+
target.writeBytes(CRLF);
742+
} else {
743+
744+
ByteBuf temporaryBuffer = target.alloc().buffer(encoder.estimateSize(item) + 6);
745+
746+
try {
747+
encodeFunction.encode(encoder, item, temporaryBuffer);
748+
ByteBufferArgument.writeByteBuf(target, temporaryBuffer);
749+
} finally {
750+
temporaryBuffer.release();
751+
}
752+
}
753+
}
754+
755+
interface EncodeFunction<T> {
756+
757+
void encode(ToByteBufEncoder<T, T> encoder, T item, ByteBuf target);
758+
759+
}
760+
774761
}

0 commit comments

Comments
 (0)