Skip to content

Commit 4739b8a

Browse files
Merge pull request #109 from fa93hws/faster-bytes-to-string
2 parents 1775a5f + 317f216 commit 4739b8a

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/main/java/com/bazel_diff/TargetHashingClient.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.*;
99
import java.util.stream.Collectors;
1010
import com.google.common.primitives.Bytes;
11+
import java.nio.charset.StandardCharsets;
1112

1213
interface TargetHashingClient {
1314
Map<String, String> hashAllBazelTargetsAndSourcefiles(Set<Path> seedFilepaths) throws IOException, NoSuchAlgorithmException;
@@ -17,6 +18,7 @@ interface TargetHashingClient {
1718
class TargetHashingClientImpl implements TargetHashingClient {
1819
private BazelClient bazelClient;
1920
private FilesClient files;
21+
private static final byte[] HEX_ARRAY = "0123456789abcdef".getBytes(StandardCharsets.US_ASCII);
2022

2123
TargetHashingClientImpl(BazelClient bazelClient, FilesClient files) {
2224
this.bazelClient = bazelClient;
@@ -136,11 +138,13 @@ private byte[] getDigestForSourceTargetName(
136138
}
137139

138140
private String convertByteArrayToString(byte[] bytes) {
139-
StringBuilder result = new StringBuilder();
140-
for (byte aByte : bytes) {
141-
result.append(String.format("%02x", aByte));
141+
byte[] hexChars = new byte[bytes.length * 2];
142+
for (int j = 0; j < bytes.length; j++) {
143+
int v = bytes[j] & 0xFF;
144+
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
145+
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
142146
}
143-
return result.toString();
147+
return new String(hexChars, StandardCharsets.UTF_8);
144148
}
145149

146150
private String getNameForTarget(BazelTarget target) {

0 commit comments

Comments
 (0)