BatchRefUpdate: Clarify some ref prefix calls

Inline the old addRefToPrefixes, since it was just a glorified addAll.
Split getPrefixes into a variant, addPrefixesTo, that doesn't allocate a
small Collection on every invocation. Use this in the tight loop of
getTakenPrefixes.

Change-Id: I25cc7feef0c8e312820d85b7ed48559da49b83d2
This commit is contained in:
Dave Borowitz 2017-07-05 09:57:01 -04:00
parent 1968b20066
commit 28adcce862
1 changed files with 11 additions and 14 deletions

View File

@ -462,7 +462,7 @@ public void execute(RevWalk walk, ProgressMonitor monitor,
break SWITCH; break SWITCH;
} }
ru.setCheckConflicting(false); ru.setCheckConflicting(false);
addRefToPrefixes(takenPrefixes, cmd.getRefName()); takenPrefixes.addAll(getPrefixes(cmd.getRefName()));
takenNames.add(cmd.getRefName()); takenNames.add(cmd.getRefName());
cmd.setResult(ru.update(walk)); cmd.setResult(ru.update(walk));
} }
@ -523,29 +523,26 @@ public void execute(RevWalk walk, ProgressMonitor monitor)
execute(walk, monitor, null); execute(walk, monitor, null);
} }
private static Collection<String> getTakenPrefixes( private static Collection<String> getTakenPrefixes(Collection<String> names) {
final Collection<String> names) {
Collection<String> ref = new HashSet<>(); Collection<String> ref = new HashSet<>();
for (String name : names) for (String name : names) {
ref.addAll(getPrefixes(name)); addPrefixesTo(name, ref);
return ref;
}
private static void addRefToPrefixes(Collection<String> prefixes,
String name) {
for (String prefix : getPrefixes(name)) {
prefixes.add(prefix);
} }
return ref;
} }
static Collection<String> getPrefixes(String s) { static Collection<String> getPrefixes(String s) {
Collection<String> ret = new HashSet<>(); Collection<String> ret = new HashSet<>();
addPrefixesTo(s, ret);
return ret;
}
static void addPrefixesTo(String s, Collection<String> out) {
int p1 = s.indexOf('/'); int p1 = s.indexOf('/');
while (p1 > 0) { while (p1 > 0) {
ret.add(s.substring(0, p1)); out.add(s.substring(0, p1));
p1 = s.indexOf('/', p1 + 1); p1 = s.indexOf('/', p1 + 1);
} }
return ret;
} }
/** /**