Skip to content

Commit

Permalink
Let Java copy arrays which it can sometimes do faster than manual loo…
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga authored and abesto committed Sep 10, 2019
1 parent eb2d725 commit 672e7c4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
20 changes: 20 additions & 0 deletions benchmarks/src/main/java/zipkin2/SpanBenchmarks.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ public byte[] deserialize_kryo() {
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()
Expand Down
16 changes: 13 additions & 3 deletions zipkin/src/main/java/zipkin2/Span.java
Original file line number Diff line number Diff line change
Expand Up @@ -659,11 +659,21 @@ public static String normalizeTraceId(String traceId) {
}
}

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);
}

Expand Down

0 comments on commit 672e7c4

Please sign in to comment.