DevTools refactor Profiler commit tree reconstruction to be iterative (#21383)
Brian Vaughn committed
Apr 28, 2021 at 16:32 UTC
3c21aa855a13707a8abe5dc6eaf2d2a7429fa762
1 file changed
+26
-33
packages/react-devtools-shared/src/devtools/views/Profiler/CommitTreeBuilder.js
+26
-33
@@ -56,7 +56,6 @@ export function getCommitTree({
56
const commitTrees = ((rootToCommitTreeMap.get(
57
rootID,
58
): any): Array<CommitTree>);
59
-
59
if (commitIndex < commitTrees.length) {
60
return commitTrees[commitIndex];
61
}
@@ -72,52 +71,46 @@ export function getCommitTree({
71
}
72
73
const {operations} = dataForRoot;
74
+ if (operations.length <= commitIndex) {
75
+ throw Error(
76
+ `getCommitTree(): Invalid commit "${commitIndex}" for root "${rootID}". There are only "${operations.length}" commits.`,
77
+ );
78
+ }
79
76
- // Commits are generated sequentially and cached.
77
- // If this is the very first commit, start with the cached snapshot and apply the first mutation.
78
- // Otherwise load (or generate) the previous commit and append a mutation to it.
79
- if (commitIndex === 0) {
80
- const nodes = new Map();
81
-
82
- // Construct the initial tree.
83
- recursivelyInitializeTree(rootID, 0, nodes, dataForRoot);
80
+ let commitTree: CommitTree = ((null: any): CommitTree);
81
+ for (let index = commitTrees.length; index <= commitIndex; index++) {
82
+ // Commits are generated sequentially and cached.
83
+ // If this is the very first commit, start with the cached snapshot and apply the first mutation.
84
+ // Otherwise load (or generate) the previous commit and append a mutation to it.
85
+ if (index === 0) {
86
+ const nodes = new Map();
87
85
- // Mutate the tree
86
- if (operations != null && commitIndex < operations.length) {
87
- const commitTree = updateTree({nodes, rootID}, operations[commitIndex]);
88
+ // Construct the initial tree.
89
+ recursivelyInitializeTree(rootID, 0, nodes, dataForRoot);
90
89
- if (__DEBUG__) {
90
- __printTree(commitTree);
91
- }
91
+ // Mutate the tree
92
+ if (operations != null && index < operations.length) {
93
+ commitTree = updateTree({nodes, rootID}, operations[index]);
94
93
- commitTrees.push(commitTree);
94
- return commitTree;
95
- }
96
- } else {
97
- const previousCommitTree = getCommitTree({
98
- commitIndex: commitIndex - 1,
99
- profilerStore,
100
- rootID,
101
- });
95
+ if (__DEBUG__) {
96
+ __printTree(commitTree);
97
+ }
98
103
- if (operations != null && commitIndex < operations.length) {
104
- const commitTree = updateTree(
105
- previousCommitTree,
106
- operations[commitIndex],
107
- );
99
+ commitTrees.push(commitTree);
100
+ }
101
+ } else {
102
+ const previousCommitTree = commitTrees[index - 1];
103
+ commitTree = updateTree(previousCommitTree, operations[index]);
104
105
if (__DEBUG__) {
106
__printTree(commitTree);
107
}
108
109
commitTrees.push(commitTree);
114
- return commitTree;
110
}
111
}
112
118
- throw Error(
119
- `getCommitTree(): Unable to reconstruct tree for root "${rootID}" and commit "${commitIndex}"`,
120
- );
113
+ return commitTree;
114
}
115
116
function recursivelyInitializeTree(