From 3c21aa855a13707a8abe5dc6eaf2d2a7429fa762 Mon Sep 17 00:00:00 2001 From: Brian Vaughn Date: Wed, 28 Apr 2021 16:32:35 -0400 Subject: [PATCH] DevTools refactor Profiler commit tree reconstruction to be iterative (#21383) --- .../views/Profiler/CommitTreeBuilder.js | 59 ++++++++----------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/packages/react-devtools-shared/src/devtools/views/Profiler/CommitTreeBuilder.js b/packages/react-devtools-shared/src/devtools/views/Profiler/CommitTreeBuilder.js index e0b5563f4ad26..1d16aba7fc292 100644 --- a/packages/react-devtools-shared/src/devtools/views/Profiler/CommitTreeBuilder.js +++ b/packages/react-devtools-shared/src/devtools/views/Profiler/CommitTreeBuilder.js @@ -56,7 +56,6 @@ export function getCommitTree({ const commitTrees = ((rootToCommitTreeMap.get( rootID, ): any): Array); - if (commitIndex < commitTrees.length) { return commitTrees[commitIndex]; } @@ -72,52 +71,46 @@ export function getCommitTree({ } const {operations} = dataForRoot; + if (operations.length <= commitIndex) { + throw Error( + `getCommitTree(): Invalid commit "${commitIndex}" for root "${rootID}". There are only "${operations.length}" commits.`, + ); + } - // Commits are generated sequentially and cached. - // If this is the very first commit, start with the cached snapshot and apply the first mutation. - // Otherwise load (or generate) the previous commit and append a mutation to it. - if (commitIndex === 0) { - const nodes = new Map(); - - // Construct the initial tree. - recursivelyInitializeTree(rootID, 0, nodes, dataForRoot); + let commitTree: CommitTree = ((null: any): CommitTree); + for (let index = commitTrees.length; index <= commitIndex; index++) { + // Commits are generated sequentially and cached. + // If this is the very first commit, start with the cached snapshot and apply the first mutation. + // Otherwise load (or generate) the previous commit and append a mutation to it. + if (index === 0) { + const nodes = new Map(); - // Mutate the tree - if (operations != null && commitIndex < operations.length) { - const commitTree = updateTree({nodes, rootID}, operations[commitIndex]); + // Construct the initial tree. + recursivelyInitializeTree(rootID, 0, nodes, dataForRoot); - if (__DEBUG__) { - __printTree(commitTree); - } + // Mutate the tree + if (operations != null && index < operations.length) { + commitTree = updateTree({nodes, rootID}, operations[index]); - commitTrees.push(commitTree); - return commitTree; - } - } else { - const previousCommitTree = getCommitTree({ - commitIndex: commitIndex - 1, - profilerStore, - rootID, - }); + if (__DEBUG__) { + __printTree(commitTree); + } - if (operations != null && commitIndex < operations.length) { - const commitTree = updateTree( - previousCommitTree, - operations[commitIndex], - ); + commitTrees.push(commitTree); + } + } else { + const previousCommitTree = commitTrees[index - 1]; + commitTree = updateTree(previousCommitTree, operations[index]); if (__DEBUG__) { __printTree(commitTree); } commitTrees.push(commitTree); - return commitTree; } } - throw Error( - `getCommitTree(): Unable to reconstruct tree for root "${rootID}" and commit "${commitIndex}"`, - ); + return commitTree; } function recursivelyInitializeTree(