Skip to content

Commit 97d8869

Browse files
committed
task: performance improvement when fetching digest for target
This changes `bazelSourceFileTargets` from a set of targets to a map of the target name to the target itself; that way we can simply do a lookup of the target name, rather than looping through the set every time we look for this (which is quite a lot, since this happens when trying to get a digest for every single target).
1 parent a093cf5 commit 97d8869

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

src/main/java/com/bazel_diff/BazelClient.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
import java.security.MessageDigest;
1313
import java.security.NoSuchAlgorithmException;
1414
import java.util.ArrayList;
15-
import java.util.HashSet;
15+
import java.util.HashMap;
1616
import java.util.List;
17-
import java.util.Set;
17+
import java.util.Map;
1818
import java.util.stream.Collectors;
1919
import java.util.Arrays;
2020

2121
interface BazelClient {
2222
List<BazelTarget> queryAllTargets() throws IOException;
23-
Set<BazelSourceFileTarget> queryAllSourcefileTargets() throws IOException, NoSuchAlgorithmException;
23+
Map<String, BazelSourceFileTarget> queryAllSourcefileTargets() throws IOException, NoSuchAlgorithmException;
2424
}
2525

2626
class BazelClientImpl implements BazelClient {
@@ -54,12 +54,12 @@ public List<BazelTarget> queryAllTargets() throws IOException {
5454
}
5555

5656
@Override
57-
public Set<BazelSourceFileTarget> queryAllSourcefileTargets() throws IOException, NoSuchAlgorithmException {
57+
public Map<String, BazelSourceFileTarget> queryAllSourcefileTargets() throws IOException, NoSuchAlgorithmException {
5858
return processBazelSourcefileTargets(performBazelQuery("kind('source file', deps(//...))"), true);
5959
}
6060

61-
private Set<BazelSourceFileTarget> processBazelSourcefileTargets(List<Build.Target> targets, Boolean readSourcefileTargets) throws IOException, NoSuchAlgorithmException {
62-
Set<BazelSourceFileTarget> sourceTargets = new HashSet<>();
61+
private Map<String, BazelSourceFileTarget> processBazelSourcefileTargets(List<Build.Target> targets, Boolean readSourcefileTargets) throws IOException, NoSuchAlgorithmException {
62+
Map<String, BazelSourceFileTarget> sourceTargets = new HashMap<>();
6363
for (Build.Target target : targets) {
6464
Build.SourceFile sourceFile = target.getSourceFile();
6565
if (sourceFile != null) {
@@ -73,7 +73,7 @@ private Set<BazelSourceFileTarget> processBazelSourcefileTargets(List<Build.Targ
7373
digest.digest().clone(),
7474
readSourcefileTargets ? workingDirectory : null
7575
);
76-
sourceTargets.add(sourceFileTarget);
76+
sourceTargets.put(sourceFileTarget.getName(), sourceFileTarget);
7777
}
7878
}
7979
return sourceTargets;

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class TargetHashingClientImpl implements TargetHashingClient {
2525

2626
@Override
2727
public Map<String, String> hashAllBazelTargetsAndSourcefiles(Set<Path> seedFilepaths) throws IOException, NoSuchAlgorithmException {
28-
Set<BazelSourceFileTarget> bazelSourcefileTargets = bazelClient.queryAllSourcefileTargets();
28+
Map<String, BazelSourceFileTarget> bazelSourcefileTargets = bazelClient.queryAllSourcefileTargets();
2929
return hashAllTargets(createSeedForFilepaths(seedFilepaths), bazelSourcefileTargets);
3030
}
3131

@@ -47,7 +47,7 @@ public Set<String> getImpactedTargets(
4747
private byte[] createDigestForTarget(
4848
BazelTarget target,
4949
Map<String, BazelRule> allRulesMap,
50-
Set<BazelSourceFileTarget> bazelSourcefileTargets,
50+
Map<String, BazelSourceFileTarget> bazelSourcefileTargets,
5151
Map<String, byte[]> ruleHashes,
5252
byte[] seedHash
5353
) throws NoSuchAlgorithmException {
@@ -73,7 +73,7 @@ private byte[] createDigestForRule(
7373
BazelRule rule,
7474
Map<String, BazelRule> allRulesMap,
7575
Map<String, byte[]> ruleHashes,
76-
Set<BazelSourceFileTarget> bazelSourcefileTargets,
76+
Map<String, BazelSourceFileTarget> bazelSourcefileTargets,
7777
byte[] seedHash
7878
) throws NoSuchAlgorithmException {
7979
byte[] existingByteArray = ruleHashes.get(rule.getName());
@@ -122,14 +122,10 @@ private byte[] createSeedForFilepaths(Set<Path> seedFilepaths) throws IOExceptio
122122

123123
private byte[] getDigestForSourceTargetName(
124124
String sourceTargetName,
125-
Set<BazelSourceFileTarget> bazelSourcefileTargets
125+
Map<String, BazelSourceFileTarget> bazelSourcefileTargets
126126
) throws NoSuchAlgorithmException {
127-
for (BazelSourceFileTarget sourceFileTarget : bazelSourcefileTargets) {
128-
if (sourceFileTarget.getName().equals(sourceTargetName)) {
129-
return sourceFileTarget.getDigest();
130-
}
131-
}
132-
return null;
127+
BazelSourceFileTarget target = bazelSourcefileTargets.get(sourceTargetName);
128+
return target != null ? target.getDigest() : null;
133129
}
134130

135131
private String convertByteArrayToString(byte[] bytes) {
@@ -150,7 +146,7 @@ private String getNameForTarget(BazelTarget target) {
150146
return null;
151147
}
152148

153-
private Map<String, String> hashAllTargets(byte[] seedHash, Set<BazelSourceFileTarget> bazelSourcefileTargets) throws IOException, NoSuchAlgorithmException {
149+
private Map<String, String> hashAllTargets(byte[] seedHash, Map<String, BazelSourceFileTarget> bazelSourcefileTargets) throws IOException, NoSuchAlgorithmException {
154150
List<BazelTarget> allTargets = bazelClient.queryAllTargets();
155151
Map<String, String> targetHashes = new HashMap<>();
156152
Map<String, byte[]> ruleHashes = new HashMap<>();

0 commit comments

Comments
 (0)