You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zipkin.apache.org by ad...@apache.org on 2019/05/11 08:43:51 UTC

[incubator-zipkin-brave] branch master updated: Remove intermediate buffer in ASCII string read.

This is an automated email from the ASF dual-hosted git repository.

adriancole pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-zipkin-brave.git


The following commit(s) were added to refs/heads/master by this push:
     new cb11df7  Remove intermediate buffer in ASCII string read.
cb11df7 is described below

commit cb11df7bc9452eef27e91063e369004754d03962
Author: Anuraag Agrawal <an...@gmail.com>
AuthorDate: Sat May 11 16:59:21 2019 +0900

    Remove intermediate buffer in ASCII string read.
---
 .../src/main/java/brave/grpc/TagContextBinaryMarshaller.java | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/instrumentation/grpc/src/main/java/brave/grpc/TagContextBinaryMarshaller.java b/instrumentation/grpc/src/main/java/brave/grpc/TagContextBinaryMarshaller.java
index a62923a..c55e421 100644
--- a/instrumentation/grpc/src/main/java/brave/grpc/TagContextBinaryMarshaller.java
+++ b/instrumentation/grpc/src/main/java/brave/grpc/TagContextBinaryMarshaller.java
@@ -2,6 +2,7 @@ package brave.grpc;
 
 import brave.internal.Platform;
 import io.grpc.Metadata.BinaryMarshaller;
+import java.nio.charset.Charset;
 import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.Map;
@@ -16,6 +17,8 @@ final class TagContextBinaryMarshaller implements BinaryMarshaller<Map<String, S
   static final byte VERSION = 0, TAG_FIELD_ID = 0;
   static final byte[] EMPTY_BYTES = {};
 
+  static final Charset US_ASCII = Charset.forName("US-ASCII");
+
   @Override
   public byte[] toBytes(Map<String, String> tagContext) {
     if (tagContext == null) {
@@ -137,11 +140,10 @@ final class TagContextBinaryMarshaller implements BinaryMarshaller<Map<String, S
 
     String readAsciiString(int length) {
       if (length == -1 || remaining() < length) return null;
-      char[] string = new char[length];
-      for (int i = 0; i < length; i++) {
-        string[i] = (char) buf[pos++];
-      }
-      return new String(string);
+
+      String result = new String(buf, pos, length, US_ASCII);
+      pos += length;
+      return result;
     }
   }
 }