Skip to content

Commit

Permalink
Addresses some feedback and uses UTF-8 decoder for short non-ascii st…
Browse files Browse the repository at this point in the history
…rings
  • Loading branch information
Adrian Cole committed May 14, 2019
1 parent 1ebab4b commit 03a993a
Show file tree
Hide file tree
Showing 20 changed files with 189 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class WriteBufferBenchmarks {
/* epoch micros timestamp */
static final long TEST_LONG = 1472470996199000L;
byte[] bytes = new byte[8];
WriteBuffer buffer = WriteBuffer.wrap(bytes, 0);
WriteBuffer buffer = WriteBuffer.wrap(bytes);

@Benchmark public int utf8SizeInBytes_chinese() {
return WriteBuffer.utf8SizeInBytes(CHINESE_UTF8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public class Proto3CodecInteropTest {
zipkin2.Annotation zipkinAnnotation = ZIPKIN_SPAN.annotations().get(0);
Span wireSpan = new Span.Builder().annotations(PROTO_SPAN.annotations).build();

ReadBuffer wireBytes = ReadBuffer.wrap(wireSpan.encode(), 0);
ReadBuffer wireBytes = ReadBuffer.wrap(wireSpan.encode());
assertThat(wireBytes.readVarint32())
.isEqualTo(ANNOTATION.key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public enum DependencyLinkBytesDecoder implements BytesDecoder<DependencyLink> {
}

@Override public boolean decode(byte[] link, Collection<DependencyLink> out) {
return JsonCodec.read(READER, ReadBuffer.wrap(link, 0), out);
return JsonCodec.read(READER, ReadBuffer.wrap(link), out);
}

@Override @Nullable public DependencyLink decodeOne(byte[] link) {
return JsonCodec.readOne(READER, ReadBuffer.wrap(link, 0));
return JsonCodec.readOne(READER, ReadBuffer.wrap(link));
}

@Override public boolean decodeList(byte[] links, Collection<DependencyLink> out) {
return JsonCodec.readList(READER, ReadBuffer.wrap(links, 0), out);
return JsonCodec.readList(READER, ReadBuffer.wrap(links), out);
}

@Override public List<DependencyLink> decodeList(byte[] links) {
Expand Down
24 changes: 12 additions & 12 deletions zipkin/src/main/java/zipkin2/codec/SpanBytesDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ public enum SpanBytesDecoder implements BytesDecoder<Span> {
}

@Override public boolean decode(byte[] span, Collection<Span> out) { // ex DependencyLinker
Span result = decodeOne(ReadBuffer.wrap(span, 0));
Span result = decodeOne(ReadBuffer.wrap(span));
if (result == null) return false;
out.add(result);
return true;
}

@Override public boolean decodeList(byte[] spans, Collection<Span> out) { // ex getTrace
return new V1JsonSpanReader().readList(ReadBuffer.wrap(spans, 0), out);
return new V1JsonSpanReader().readList(ReadBuffer.wrap(spans), out);
}

@Override public boolean decodeList(ByteBuffer spans, Collection<Span> out) {
return new V1JsonSpanReader().readList(ReadBuffer.wrapUnsafe(spans), out);
}

@Override @Nullable public Span decodeOne(byte[] span) {
return decodeOne(ReadBuffer.wrap(span, 0));
return decodeOne(ReadBuffer.wrap(span));
}

@Override @Nullable public Span decodeOne(ByteBuffer span) {
Expand Down Expand Up @@ -86,19 +86,19 @@ Span decodeOne(ReadBuffer buffer) {
}

@Override public boolean decode(byte[] span, Collection<Span> out) { // ex DependencyLinker
return ThriftCodec.read(ReadBuffer.wrap(span, 0), out);
return ThriftCodec.read(ReadBuffer.wrap(span), out);
}

@Override public boolean decodeList(byte[] spans, Collection<Span> out) { // ex getTrace
return ThriftCodec.readList(ReadBuffer.wrap(spans, 0), out);
return ThriftCodec.readList(ReadBuffer.wrap(spans), out);
}

@Override public boolean decodeList(ByteBuffer spans, Collection<Span> out) {
return ThriftCodec.readList(ReadBuffer.wrapUnsafe(spans), out);
}

@Override @Nullable public Span decodeOne(byte[] span) {
return ThriftCodec.readOne(ReadBuffer.wrap(span, 0));
return ThriftCodec.readOne(ReadBuffer.wrap(span));
}

@Override @Nullable public Span decodeOne(ByteBuffer span) {
Expand All @@ -120,19 +120,19 @@ Span decodeOne(ReadBuffer buffer) {
}

@Override public boolean decode(byte[] span, Collection<Span> out) { // ex DependencyLinker
return JsonCodec.read(new V2SpanReader(), ReadBuffer.wrap(span, 0), out);
return JsonCodec.read(new V2SpanReader(), ReadBuffer.wrap(span), out);
}

@Override public boolean decodeList(byte[] spans, Collection<Span> out) { // ex getTrace
return JsonCodec.readList(new V2SpanReader(), ReadBuffer.wrap(spans, 0), out);
return JsonCodec.readList(new V2SpanReader(), ReadBuffer.wrap(spans), out);
}

@Override public boolean decodeList(ByteBuffer spans, Collection<Span> out) {
return JsonCodec.readList(new V2SpanReader(), ReadBuffer.wrapUnsafe(spans), out);
}

@Override @Nullable public Span decodeOne(byte[] span) {
return JsonCodec.readOne(new V2SpanReader(), ReadBuffer.wrap(span, 0));
return JsonCodec.readOne(new V2SpanReader(), ReadBuffer.wrap(span));
}

@Override @Nullable public Span decodeOne(ByteBuffer span) {
Expand All @@ -153,19 +153,19 @@ Span decodeOne(ReadBuffer buffer) {
}

@Override public boolean decode(byte[] span, Collection<Span> out) { // ex DependencyLinker
return Proto3Codec.read(ReadBuffer.wrap(span, 0), out);
return Proto3Codec.read(ReadBuffer.wrap(span), out);
}

@Override public boolean decodeList(byte[] spans, Collection<Span> out) { // ex getTrace
return Proto3Codec.readList(ReadBuffer.wrap(spans, 0), out);
return Proto3Codec.readList(ReadBuffer.wrap(spans), out);
}

@Override public boolean decodeList(ByteBuffer spans, Collection<Span> out) {
return Proto3Codec.readList(ReadBuffer.wrapUnsafe(spans), out);
}

@Override @Nullable public Span decodeOne(byte[] span) {
return Proto3Codec.readOne(ReadBuffer.wrap(span, 0));
return Proto3Codec.readOne(ReadBuffer.wrap(span));
}

@Override @Nullable public Span decodeOne(ByteBuffer span) {
Expand Down
2 changes: 1 addition & 1 deletion zipkin/src/main/java/zipkin2/internal/Dependencies.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static Dependencies fromThrift(ByteBuffer bytes) {
/** Writes the current instance in TBinaryProtocol */
public ByteBuffer toThrift() {
byte[] result = new byte[sizeInBytes()];
write(WriteBuffer.wrap(result, 0));
write(WriteBuffer.wrap(result));
return ByteBuffer.wrap(result);
}

Expand Down
4 changes: 2 additions & 2 deletions zipkin/src/main/java/zipkin2/internal/JsonCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static <T> int sizeInBytes(WriteBuffer.Writer<T> writer, List<T> value) {
/** Inability to encode is a programming bug. */
public static <T> byte[] write(WriteBuffer.Writer<T> writer, T value) {
byte[] result = new byte[writer.sizeInBytes(value)];
WriteBuffer b = WriteBuffer.wrap(result, 0);
WriteBuffer b = WriteBuffer.wrap(result);
try {
writer.write(value, b);
} catch (RuntimeException e) {
Expand Down Expand Up @@ -198,7 +198,7 @@ public static <T> byte[] write(WriteBuffer.Writer<T> writer, T value) {
public static <T> byte[] writeList(WriteBuffer.Writer<T> writer, List<T> value) {
if (value.isEmpty()) return new byte[] {'[', ']'};
byte[] result = new byte[sizeInBytes(writer, value)];
writeList(writer, value, WriteBuffer.wrap(result, 0));
writeList(writer, value, WriteBuffer.wrap(result));
return result;
}

Expand Down
17 changes: 17 additions & 0 deletions zipkin/src/main/java/zipkin2/internal/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package zipkin2.internal;

import java.io.IOException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
import org.jvnet.animal_sniffer.IgnoreJRERequirement;

/**
Expand Down Expand Up @@ -48,6 +50,21 @@ public static char[] shortStringBuffer() {
return shortStringBuffer;
}

static final ThreadLocal<CharsetDecoder> UTF8_DECODER = new ThreadLocal<>();

/**
* Returns a {@link ThreadLocal} reused {@code CharsetDecoder} for use when decoding bytes into
* UTF-8 strings or characters. The decoder must never be leaked outside the method.
*/
public static CharsetDecoder utf8Decoder() {
CharsetDecoder utf8Decoder = UTF8_DECODER.get();
if (utf8Decoder == null) {
utf8Decoder = StandardCharsets.UTF_8.newDecoder();
UTF8_DECODER.set(utf8Decoder);
}
return utf8Decoder;
}

public RuntimeException uncheckedIOException(IOException e) {
return new RuntimeException(e);
}
Expand Down
4 changes: 2 additions & 2 deletions zipkin/src/main/java/zipkin2/internal/Proto3SpanWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public byte[] writeList(List<Span> spans) {
sizeInBytes += sizeOfLengthDelimitedField(sizeOfValue);
}
byte[] result = new byte[sizeInBytes];
WriteBuffer writeBuffer = WriteBuffer.wrap(result, 0);
WriteBuffer writeBuffer = WriteBuffer.wrap(result);
for (int i = 0; i < lengthOfSpans; i++) {
writeSpan(spans.get(i), sizeOfValues[i], writeBuffer);
}
Expand All @@ -62,7 +62,7 @@ public byte[] writeList(List<Span> spans) {
byte[] write(Span onlySpan) {
int sizeOfValue = SPAN.sizeOfValue(onlySpan);
byte[] result = new byte[sizeOfLengthDelimitedField(sizeOfValue)];
writeSpan(onlySpan, sizeOfValue, WriteBuffer.wrap(result, 0));
writeSpan(onlySpan, sizeOfValue, WriteBuffer.wrap(result));
return result;
}

Expand Down
Loading

0 comments on commit 03a993a

Please sign in to comment.