Skip to content

Commit bbf4124

Browse files
Caching change proneness
Caching change proneness to speed up processing - seems to help some
1 parent 6380f3e commit bbf4124

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

change-proneness-ranker/src/main/java/org/hjug/git/ChangePronenessRanker.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package org.hjug.git;
22

33
import java.io.IOException;
4-
import java.util.Comparator;
5-
import java.util.List;
6-
import java.util.TreeMap;
4+
import java.util.*;
75
import lombok.extern.slf4j.Slf4j;
86
import org.eclipse.jgit.api.errors.GitAPIException;
97
import org.eclipse.jgit.lib.Repository;
@@ -12,6 +10,7 @@
1210
public class ChangePronenessRanker {
1311

1412
private final TreeMap<Integer, Integer> changeCountsByTimeStamps = new TreeMap<>();
13+
private final Map<String, ScmLogInfo> cachedScmLogInfos = new HashMap<>();
1514

1615
public ChangePronenessRanker(Repository repository, GitLogReader repositoryLogReader) {
1716
try {
@@ -24,12 +23,18 @@ public ChangePronenessRanker(Repository repository, GitLogReader repositoryLogRe
2423

2524
public void rankChangeProneness(List<ScmLogInfo> scmLogInfos) {
2625
for (ScmLogInfo scmLogInfo : scmLogInfos) {
27-
int commitsInRepositorySinceCreation =
28-
changeCountsByTimeStamps.tailMap(scmLogInfo.getEarliestCommit()).values().stream()
29-
.mapToInt(i -> i)
30-
.sum();
31-
32-
scmLogInfo.setChangeProneness((float) scmLogInfo.getCommitCount() / commitsInRepositorySinceCreation);
26+
if (!cachedScmLogInfos.containsKey(scmLogInfo.getPath())) {
27+
int commitsInRepositorySinceCreation =
28+
changeCountsByTimeStamps.tailMap(scmLogInfo.getEarliestCommit()).values().stream()
29+
.mapToInt(i -> i)
30+
.sum();
31+
32+
scmLogInfo.setChangeProneness((float) scmLogInfo.getCommitCount() / commitsInRepositorySinceCreation);
33+
cachedScmLogInfos.put(scmLogInfo.getPath(), scmLogInfo);
34+
} else {
35+
scmLogInfo.setChangeProneness(
36+
cachedScmLogInfos.get(scmLogInfo.getPath()).getChangeProneness());
37+
}
3338
}
3439

3540
scmLogInfos.sort(Comparator.comparing(ScmLogInfo::getChangeProneness));

0 commit comments

Comments
 (0)