diff --git a/src/core/graph.js b/src/core/graph.js index 1f4933e..fd8bcd8 100644 --- a/src/core/graph.js +++ b/src/core/graph.js @@ -338,6 +338,28 @@ export class Graph { ); return result; } + + /** + * Equivalent to + * + * graphs.reduce((g, h) => Graph.mergeConservative(g, h), new Graph()), + * + * but uses a mutable accumulator for improved performance. + */ + static mergeManyConservative( + graphs: $ReadOnlyArray, $Subtype>> + ): Graph { + const result = new Graph(); + graphs.forEach((graph) => { + graph.nodes().forEach((node) => { + result.addNode(node); + }); + graph.edges().forEach((edge) => { + result.addEdge(edge); + }); + }); + return result; + } } export function edgeID(src: Address, dst: Address): string { diff --git a/src/plugins/git/createGraph.js b/src/plugins/git/createGraph.js index 7c1c7eb..264d5f5 100644 --- a/src/plugins/git/createGraph.js +++ b/src/plugins/git/createGraph.js @@ -41,7 +41,7 @@ class GitGraphCreator { const treeAndNameToSubmoduleUrls = this.treeAndNameToSubmoduleUrls( repository ); - const graphs = [ + return Graph.mergeManyConservative([ ...Object.keys(repository.commits).map((hash) => this.commitGraph(repository.commits[hash]) ), @@ -49,17 +49,7 @@ class GitGraphCreator { this.treeGraph(repository.trees[hash], treeAndNameToSubmoduleUrls) ), this.becomesEdges(repository), - ]; - const result = new Graph(); - graphs.forEach((g) => { - g.nodes().forEach((node) => { - result.addNode(node); - }); - g.edges().forEach((edge) => { - result.addEdge(edge); - }); - }); - return result; + ]); } treeAndNameToSubmoduleUrls(repository: Repository) {