This is an automated email from the ASF dual-hosted git repository. anuraaga 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 44719d4 Reuse buffer when converting long to hex. (#907) 44719d4 is described below commit 44719d4e5298a4be9b88a95dbd1e28a6e3758065 Author: Anuraag Agrawal <anura...@gmail.com> AuthorDate: Sun May 12 18:37:36 2019 +0900 Reuse buffer when converting long to hex. (#907) * Reuse buffer when converting long to hex. * Move buffer to separate class to not deal with platformness. --- brave/src/main/java/brave/internal/HexCodec.java | 4 +-- .../java/brave/internal/RecyclableBuffers.java | 36 ++++++++++++++++++++++ .../main/java/brave/propagation/TraceContext.java | 3 +- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/brave/src/main/java/brave/internal/HexCodec.java b/brave/src/main/java/brave/internal/HexCodec.java index 76b1c1a..595e67c 100644 --- a/brave/src/main/java/brave/internal/HexCodec.java +++ b/brave/src/main/java/brave/internal/HexCodec.java @@ -52,9 +52,9 @@ public final class HexCodec { /** Inspired by {@code okio.Buffer.writeLong} */ public static String toLowerHex(long v) { - char[] data = new char[16]; + char[] data = RecyclableBuffers.idBuffer(); writeHexLong(data, 0, v); - return new String(data); + return new String(data, 0, 16); } /** Inspired by {@code okio.Buffer.writeLong} */ diff --git a/brave/src/main/java/brave/internal/RecyclableBuffers.java b/brave/src/main/java/brave/internal/RecyclableBuffers.java new file mode 100644 index 0000000..22e20d8 --- /dev/null +++ b/brave/src/main/java/brave/internal/RecyclableBuffers.java @@ -0,0 +1,36 @@ +/* + * Copyright 2019 The OpenZipkin Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package brave.internal; + +public final class RecyclableBuffers { + + private static final ThreadLocal<char[]> ID_BUFFER = new ThreadLocal<>(); + + /** + * Returns a {@link ThreadLocal} reused {@code char[]} for use when decoding bytes into an ID hex + * string. The buffer should be immediately copied into a {@link String} after decoding within the + * same method. + */ + public static char[] idBuffer() { + char[] idBuffer = ID_BUFFER.get(); + if (idBuffer == null) { + idBuffer = new char[32]; + ID_BUFFER.set(idBuffer); + } + return idBuffer; + } + + private RecyclableBuffers() {} +} diff --git a/brave/src/main/java/brave/propagation/TraceContext.java b/brave/src/main/java/brave/propagation/TraceContext.java index 7e7c809..bf509f8 100644 --- a/brave/src/main/java/brave/propagation/TraceContext.java +++ b/brave/src/main/java/brave/propagation/TraceContext.java @@ -4,6 +4,7 @@ import brave.Span; import brave.internal.InternalPropagation; import brave.internal.Nullable; import brave.internal.Platform; +import brave.internal.RecyclableBuffers; import java.lang.ref.WeakReference; import java.util.Collections; import java.util.List; @@ -185,7 +186,7 @@ public final class TraceContext extends SamplingFlags { String r = traceIdString; if (r == null) { if (traceIdHigh != 0) { - char[] result = new char[32]; + char[] result = RecyclableBuffers.idBuffer(); writeHexLong(result, 0, traceIdHigh); writeHexLong(result, 16, traceId); r = new String(result);