Skip to content

Commit

Permalink
JCBC-1062: Optimize LegacyTranscoder encoding for strings
Browse files Browse the repository at this point in the history
Motivation
----------
The LegacyTranscoder still uses the slower JVM-based encoding for
strings and it rather should use the same optimized encoding
path which we use for RawJsonDocument and others.

Modifications
-------------
This code adds a "fastpath" for the string encoding and moves
all the other options into a nested else block so that the buffer
is only allocated if needed (and not twice w/ overridden by the
optimized method).

Result
------
Faster encoding for the string path.

Change-Id: Id67e7f7b905491b3bb5be5f1bf0552a3fd264b79
Reviewed-on: http://review.couchbase.org/75774
Reviewed-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Tested-by: Michael Nitschinger <michael@nitschinger.at>
Reviewed-by: Subhashni Balakrishnan <b.subhashni@gmail.com>
  • Loading branch information
daschl committed Mar 28, 2017
1 parent e772eb2 commit 5666c32
Showing 1 changed file with 33 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,38 +141,42 @@ protected Tuple2<ByteBuf, Integer> doEncode(LegacyDocument document)

int flags = 0;
Object content = document.content();
ByteBuf encoded = Unpooled.buffer();

ByteBuf encoded;
if (content instanceof String) {
encoded.writeBytes(((String) content).getBytes(CharsetUtil.UTF_8));
} else if (content instanceof Long) {
flags |= SPECIAL_LONG;
encoded.writeBytes(encodeNum((Long) content, 8));
} else if (content instanceof Integer) {
flags |= SPECIAL_INT;
encoded.writeBytes(encodeNum((Integer) content, 4));
} else if (content instanceof Boolean) {
flags |= SPECIAL_BOOLEAN;
boolean b = (Boolean) content;
encoded = Unpooled.buffer().writeByte(b ? '1' : '0');
} else if (content instanceof Date) {
flags |= SPECIAL_DATE;
encoded.writeBytes(encodeNum(((Date) content).getTime(), 8));
} else if (content instanceof Byte) {
flags |= SPECIAL_BYTE;
encoded.writeByte((Byte) content);
} else if (content instanceof Float) {
flags |= SPECIAL_FLOAT;
encoded.writeBytes(encodeNum(Float.floatToRawIntBits((Float) content), 4));
} else if (content instanceof Double) {
flags |= SPECIAL_DOUBLE;
encoded.writeBytes(encodeNum(Double.doubleToRawLongBits((Double) content), 8));
} else if (content instanceof byte[]) {
flags |= SPECIAL_BYTEARRAY;
encoded.writeBytes((byte[]) content);
encoded = TranscoderUtils.encodeStringAsUtf8((String) content);
} else {
flags |= SERIALIZED;
encoded.writeBytes(serialize(content));
encoded = Unpooled.buffer();

if (content instanceof Long) {
flags |= SPECIAL_LONG;
encoded.writeBytes(encodeNum((Long) content, 8));
} else if (content instanceof Integer) {
flags |= SPECIAL_INT;
encoded.writeBytes(encodeNum((Integer) content, 4));
} else if (content instanceof Boolean) {
flags |= SPECIAL_BOOLEAN;
boolean b = (Boolean) content;
encoded = Unpooled.buffer().writeByte(b ? '1' : '0');
} else if (content instanceof Date) {
flags |= SPECIAL_DATE;
encoded.writeBytes(encodeNum(((Date) content).getTime(), 8));
} else if (content instanceof Byte) {
flags |= SPECIAL_BYTE;
encoded.writeByte((Byte) content);
} else if (content instanceof Float) {
flags |= SPECIAL_FLOAT;
encoded.writeBytes(encodeNum(Float.floatToRawIntBits((Float) content), 4));
} else if (content instanceof Double) {
flags |= SPECIAL_DOUBLE;
encoded.writeBytes(encodeNum(Double.doubleToRawLongBits((Double) content), 8));
} else if (content instanceof byte[]) {
flags |= SPECIAL_BYTEARRAY;
encoded.writeBytes((byte[]) content);
} else {
flags |= SERIALIZED;
encoded.writeBytes(serialize(content));
}
}

if (encoded.readableBytes() >= compressionThreshold) {
Expand Down

0 comments on commit 5666c32

Please sign in to comment.