Skip to content

Commit 58c64d5

Browse files
committed
Try to use an interval tree in computePositions
1 parent aeba1c7 commit 58c64d5

File tree

1 file changed

+12
-28
lines changed

1 file changed

+12
-28
lines changed

src/renderer/helpers/commit-graph.ts

+12-28
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,6 @@ export class CommitGraph {
5252
}
5353

5454
computePositions(repo: RepoState) {
55-
function mergeSets(sets: Set<number>[]) {
56-
return new Set<number>(function*() {
57-
for (let set of sets) {
58-
yield* set;
59-
}
60-
}());
61-
}
62-
6355
function insertCommit(commitSha: string, j: number, forbiddenIndices: Set<number>) {
6456
// Try to insert as close as possible to i
6557
// replace i by j
@@ -83,18 +75,14 @@ export class CommitGraph {
8375
const headSha = repo.headCommit ? repo.headCommit.sha() : null;
8476
let i = 1;
8577
const branches: (string | null)[] = ['index'];
86-
const activeNodes = new Map<string, Set<number>>();
87-
const activeNodesQueue = new FastPriorityQueue<[number, string]>((lhs, rhs) => lhs[0] < rhs[0]);
88-
activeNodes.set('index', new Set<number>());
89-
if (headSha) {
90-
activeNodesQueue.add([repo.shaToIndex.get(headSha)!, 'index']);
91-
}
78+
const edges = new IntervalTree<number>();
9279
for (let commit of repo.commits) {
9380
let j = -1;
9481
const commitSha = commit.sha();
9582
const children = repo.children.get(commit.sha())!;
9683
// Compute forbidden indices
97-
const forbiddenIndices = mergeSets(children.filter((childSha) => repo.parents.get(childSha)![0] !== commitSha).map((childSha) => activeNodes.get(childSha)!));
84+
const iMin = Math.min(...children.filter((childSha) => repo.parents.get(childSha)![0] !== commitSha).map((childSha) => this.positions.get(childSha)![0])!, i);
85+
const forbiddenIndices = new Set<number>(edges.search(iMin, i));
9886
// Find a commit to replace
9987
let commitToReplace: string | null = null;
10088
let jCommitToReplace = Infinity;
@@ -127,25 +115,21 @@ export class CommitGraph {
127115
j = insertCommit(commitSha, 0, new Set());
128116
}
129117
}
130-
// Remove useless active nodes
131-
while (!activeNodesQueue.isEmpty() && activeNodesQueue.peek()[0] < i) {
132-
const sha = activeNodesQueue.poll()[1];
133-
activeNodes.delete(sha);
134-
}
135-
// Upddate the active nodes
136-
const jToAdd = [j, ...children.filter((childSha) => repo.parents.get(childSha)![0] === commitSha).map((childSha) => this.positions.get(childSha)![1])];
137-
for (let activeNode of activeNodes.values()) {
138-
jToAdd.forEach((j) => activeNode.add(j));
139-
}
140-
activeNodes.set(commitSha, new Set<number>());
141-
const iRemove = Math.max(...repo.parents.get(commitSha)!.map((parentSha) => repo.shaToIndex.get(parentSha)!));
142-
activeNodesQueue.add([iRemove, commitSha]);
143118
// Remove children from active branches
144119
for (let childSha of children) {
145120
if (childSha != commitToReplace && repo.parents.get(childSha)![0] === commitSha) {
146121
branches[branches.indexOf(childSha)] = null; // Use positions
147122
}
148123
}
124+
// Add edges
125+
for (let childSha of children) {
126+
const [iChild, jChild] = this.positions.get(childSha)!;
127+
if (repo.parents.get(childSha)![0] === commitSha) {
128+
edges.insert(iChild, i - 1, jChild);
129+
} else {
130+
edges.insert(iChild, i - 1, j);
131+
}
132+
}
149133
// Finally set the position
150134
this.positions.set(commitSha, [i, j, repo.stashes.has(commitSha) ? NodeType.Stash : NodeType.Commit]);
151135
++i;

0 commit comments

Comments
 (0)