Skip to content

Commit

Permalink
DevTools refactor Profiler commit tree reconstruction to be iterative (
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn authored Apr 28, 2021
1 parent 9e9dac6 commit 3c21aa8
Showing 1 changed file with 26 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export function getCommitTree({
const commitTrees = ((rootToCommitTreeMap.get(
rootID,
): any): Array<CommitTree>);

if (commitIndex < commitTrees.length) {
return commitTrees[commitIndex];
}
Expand All @@ -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(
Expand Down

0 comments on commit 3c21aa8

Please sign in to comment.