zipkin-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anura...@apache.org
Subject [incubator-zipkin] branch master updated: Let Java copy arrays which it can sometimes do faster than manual loops. (#2591)
Date Tue, 14 May 2019 07:42:53 GMT
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.git


The following commit(s) were added to refs/heads/master by this push:
     new 1f152a4  Let Java copy arrays which it can sometimes do faster than manual loops.
(#2591)
1f152a4 is described below

commit 1f152a418178ef4da75b0f99cbef5674d18114ae
Author: Anuraag Agrawal <anuraaga@gmail.com>
AuthorDate: Tue May 14 16:42:49 2019 +0900

    Let Java copy arrays which it can sometimes do faster than manual loops. (#2591)
---
 benchmarks/src/main/java/zipkin2/SpanBenchmarks.java | 20 ++++++++++++++++++++
 zipkin/src/main/java/zipkin2/Span.java               | 16 +++++++++++++---
 2 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/benchmarks/src/main/java/zipkin2/SpanBenchmarks.java b/benchmarks/src/main/java/zipkin2/SpanBenchmarks.java
index a30a1d2..182c7ae 100644
--- a/benchmarks/src/main/java/zipkin2/SpanBenchmarks.java
+++ b/benchmarks/src/main/java/zipkin2/SpanBenchmarks.java
@@ -143,6 +143,26 @@ public class SpanBenchmarks {
     return output.getBuffer();
   }
 
+  @Benchmark
+  public String padLeft_1Char() {
+    return Span.padLeft("1", 16);
+  }
+
+  @Benchmark
+  public String padLeft_15Chars() {
+    return Span.padLeft("123456789012345", 16);
+  }
+
+  @Benchmark
+  public String padLeft_17Chars() {
+    return Span.padLeft("12345678901234567", 32);
+  }
+
+  @Benchmark
+  public String padLeft_31Chars() {
+    return Span.padLeft("1234567890123456789012345678901", 32);
+  }
+
   // Convenience main entry-point
   public static void main(String[] args) throws RunnerException {
     Options opt = new OptionsBuilder()
diff --git a/zipkin/src/main/java/zipkin2/Span.java b/zipkin/src/main/java/zipkin2/Span.java
index 6ea685d..8d22bce 100644
--- a/zipkin/src/main/java/zipkin2/Span.java
+++ b/zipkin/src/main/java/zipkin2/Span.java
@@ -659,11 +659,21 @@ public final class Span implements Serializable { // for Spark and Flink
jobs
     }
   }
 
+  static final String THIRTY_TWO_ZEROS;
+  static {
+    char[] zeros = new char[32];
+    Arrays.fill(zeros, '0');
+    THIRTY_TWO_ZEROS = new String(zeros);
+  }
+
   static String padLeft(String id, int desiredLength) {
+    int length = id.length();
+    int numZeros = desiredLength - length;
+
     char[] data = Platform.shortStringBuffer();
-    int i = 0, length = id.length(), offset = desiredLength - length;
-    for (; i < offset; i++) data[i] = '0';
-    for (int j = 0; j < length; j++) data[i++] = id.charAt(j);
+    THIRTY_TWO_ZEROS.getChars(0, numZeros, data, 0);
+    id.getChars(0, length, data, numZeros);
+
     return new String(data, 0, desiredLength);
   }
 


Mime
View raw message