Skip to content

Commit

Permalink
remove dependency between aggrow and stackRegistry
Browse files Browse the repository at this point in the history
Reviewed By: bnham

Differential Revision: D3876267

fbshipit-source-id: 2ad7e70445f3f4641cde554e94de000403368233
  • Loading branch information
cwdick authored and Facebook Github Bot committed Oct 6, 2016
1 parent 1502e66 commit 217a444
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 82 deletions.
4 changes: 2 additions & 2 deletions local-cli/server/middleware/heapCapture/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
SHELL := /bin/bash

all:
NODE_PATH="../../../../node_modules/" babel --presets babel-preset-react-native -d out src
for f in out/*.js; do echo "// @generated" >> $$f; done
NODE_PATH="../../../../node_modules/" babel --presets babel-preset-react-native --source-maps inline -d out src
for f in out/*.js; do echo -e "\n// @generated" >> $$f; done

watch:
NODE_PATH="../../../../node_modules/" babel --watch --presets babel-preset-react-native -d out src
48 changes: 26 additions & 22 deletions local-cli/server/middleware/heapCapture/out/aggrow.js

Large diffs are not rendered by default.

46 changes: 28 additions & 18 deletions local-cli/server/middleware/heapCapture/out/heapCapture.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion local-cli/server/middleware/heapCapture/out/table.js

Large diffs are not rendered by default.

44 changes: 23 additions & 21 deletions local-cli/server/middleware/heapCapture/src/aggrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,16 @@ function stackData(stackIdMap, maxDepth) { // eslint-disable-line no-unused-vars
};
}

function stackRegistry(interner) { // eslint-disable-line no-unused-vars
function stackRegistry() { // eslint-disable-line no-unused-vars
return {
root: { id: 0 },
nodeCount: 1,
insert: function insertNode(parent, label) {
const labelId = interner.intern(label);
let node = parent[labelId];
insert: function insertNode(parent, frameId) {
let node = parent[frameId];
if (node === undefined) {
node = { id: this.nodeCount };
this.nodeCount++;
parent[labelId] = node;
parent[frameId] = node;
}
return node;
},
Expand Down Expand Up @@ -116,7 +115,7 @@ function stackRegistry(interner) { // eslint-disable-line no-unused-vars
};
}

function aggrow(strings, stacks, numRows) { // eslint-disable-line no-unused-vars
function aggrow(numRows) { // eslint-disable-line no-unused-vars
// expander ID definitions
const FIELD_EXPANDER_ID_MIN = 0x0000;
const FIELD_EXPANDER_ID_MAX = 0x7fff;
Expand All @@ -142,17 +141,17 @@ function aggrow(strings, stacks, numRows) { // eslint-disable-line no-unused-var
const NODE_REPOSITION_BIT = 0x0008; // children need position
const NODE_INDENT_SHIFT = 16;

function calleeFrameGetter(stack, depth) {
function calleeFrameIdGetter(stack, depth) {
return stack[depth];
}

function callerFrameGetter(stack, depth) {
function callerFrameIdGetter(stack, depth) {
return stack[stack.length - depth - 1];
}

function createStackComparers(stackGetter, frameGetter) {
const comparers = new Array(stacks.maxDepth);
for (let depth = 0; depth < stacks.maxDepth; depth++) {
function createStackComparers(stackGetter, frameIdGetter, maxStackDepth) {
const comparers = new Array(maxStackDepth);
for (let depth = 0; depth < maxStackDepth; depth++) {
const captureDepth = depth; // NB: to capture depth per loop iteration
comparers[depth] = function calleeStackComparer(rowA, rowB) {
const a = stackGetter(rowA);
Expand All @@ -166,7 +165,7 @@ function aggrow(strings, stacks, numRows) { // eslint-disable-line no-unused-var
} else if (b.length <= captureDepth) {
return 1;
}
return frameGetter(a, captureDepth) - frameGetter(b, captureDepth);
return frameIdGetter(a, captureDepth) - frameIdGetter(b, captureDepth);
};
}
return comparers;
Expand Down Expand Up @@ -325,6 +324,7 @@ function aggrow(strings, stacks, numRows) { // eslint-disable-line no-unused-var
function addChildrenWithStackExpander(row, expander, activeIndex, depth, nextActiveIndex) {
const rowIndices = row.indices;
const stackGetter = expander.stackGetter;
const frameIdGetter = expander.frameIdGetter;
const frameGetter = expander.frameGetter;
const comparer = expander.comparers[depth];
const expandNextFrame = activeIndex | ((depth + 1) << ACTIVE_EXPANDER_FRAME_SHIFT);
Expand Down Expand Up @@ -357,10 +357,10 @@ function aggrow(strings, stacks, numRows) { // eslint-disable-line no-unused-var
let end = begin + 1;
while (end < rowIndices.length) {
const endStack = stackGetter(rowIndices[end]);
if (frameGetter(beginStack, depth) !== frameGetter(endStack, depth)) {
if (frameIdGetter(beginStack, depth) !== frameIdGetter(endStack, depth)) {
row.children.push(createTreeNode(
row,
columnName + strings.get(frameGetter(beginStack, depth)),
columnName + frameGetter(frameIdGetter(beginStack, depth)),
rowIndices.subarray(begin, end),
expandNextFrame));
begin = end;
Expand All @@ -370,7 +370,7 @@ function aggrow(strings, stacks, numRows) { // eslint-disable-line no-unused-var
}
row.children.push(createTreeNode(
row,
columnName + strings.get(frameGetter(beginStack, depth)),
columnName + frameGetter(frameIdGetter(beginStack, depth)),
rowIndices.subarray(begin, end),
expandNextFrame));
}
Expand Down Expand Up @@ -418,27 +418,29 @@ function aggrow(strings, stacks, numRows) { // eslint-disable-line no-unused-var
});
return FIELD_EXPANDER_ID_MIN + state.fieldExpanders.length - 1;
},
addCalleeStackExpander: function addCalleeStackExpander(name, stackGetter) {
addCalleeStackExpander: function addCalleeStackExpander(name, maxStackDepth, stackGetter, frameGetter) {
if (STACK_EXPANDER_ID_MIN + state.fieldExpanders.length >= STACK_EXPANDER_ID_MAX) {
throw 'too many stack expanders!';
}
state.stackExpanders.push({
name: name, // name for column
stackGetter: stackGetter, // row index -> stack array
comparers: createStackComparers(stackGetter, calleeFrameGetter), // depth -> comparer
frameGetter: calleeFrameGetter, // (stack, depth) -> string id
comparers: createStackComparers(stackGetter, calleeFrameIdGetter, maxStackDepth), // depth -> comparer
frameIdGetter: calleeFrameIdGetter, // (stack, depth) -> string id
frameGetter: frameGetter,
});
return STACK_EXPANDER_ID_MIN + state.stackExpanders.length - 1;
},
addCallerStackExpander: function addCallerStackExpander(name, stackGetter) {
addCallerStackExpander: function addCallerStackExpander(name, maxStackDepth, stackGetter, frameGetter) {
if (STACK_EXPANDER_ID_MIN + state.fieldExpanders.length >= STACK_EXPANDER_ID_MAX) {
throw 'too many stack expanders!';
}
state.stackExpanders.push({
name: name,
stackGetter: stackGetter,
comparers: createStackComparers(stackGetter, callerFrameGetter),
frameGetter: callerFrameGetter,
comparers: createStackComparers(stackGetter, callerFrameIdGetter, maxStackDepth),
frameIdGetter: callerFrameIdGetter,
frameGetter: frameGetter,
});
return STACK_EXPANDER_ID_MIN + state.stackExpanders.length - 1;
},
Expand Down
42 changes: 25 additions & 17 deletions local-cli/server/middleware/heapCapture/src/heapCapture.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,28 +145,28 @@ function getInternalInstanceName(visitor) {
return '#unknown';
}

function buildReactComponentTree(visitor, registry) {
function buildReactComponentTree(visitor, registry, strings) {
const ref = visitor.getRef();
if (ref.reactTree || ref.reactParent === undefined) {
return; // has one or doesn't need one
}
const parentVisitor = ref.reactParent;
if (parentVisitor === null) {
ref.reactTree = registry.insert(registry.root, getInternalInstanceName(visitor));
ref.reactTree = registry.insert(registry.root, strings.intern(getInternalInstanceName(visitor)));
} else if (parentVisitor) {
const parentRef = parentVisitor.getRef();
buildReactComponentTree(parentVisitor, registry);
buildReactComponentTree(parentVisitor, registry, strings);
let relativeName = getInternalInstanceName(visitor);
if (ref.reactKey) {
relativeName = ref.reactKey + ': ' + relativeName;
}
ref.reactTree = registry.insert(parentRef.reactTree, relativeName);
ref.reactTree = registry.insert(parentRef.reactTree, strings.intern(relativeName));
} else {
throw 'non react instance parent of react instance';
}
}

function markReactComponentTree(refs, registry) {
function markReactComponentTree(refs, registry, strings) {
// annotate all refs that are react internal instances with their parent and name
// ref.reactParent = visitor that points to parent instance,
// null if we know it's an instance, but don't have a parent yet
Expand Down Expand Up @@ -201,7 +201,7 @@ function markReactComponentTree(refs, registry) {
// build tree of react internal instances (since that's what has the structure)
// fill in ref.reactTree = path registry node
forEachRef(refs, (visitor) => {
buildReactComponentTree(visitor, registry);
buildReactComponentTree(visitor, registry, strings);
});
// hook in components by looking at their _reactInternalInstance fields
forEachRef(refs, (visitor) => {
Expand Down Expand Up @@ -253,14 +253,14 @@ function markModules(refs) {
});
}

function registerPathToRoot(refs, registry) {
markReactComponentTree(refs, registry);
function registerPathToRoot(refs, registry, strings) {
markReactComponentTree(refs, registry, strings);
markModules(refs);
let breadth = [];
forEachRef(refs, (visitor) => {
const ref = visitor.getRef();
if (ref.type === 'CallbackGlobalObject') {
ref.rootPath = registry.insert(registry.root, ref.type);
ref.rootPath = registry.insert(registry.root, strings.intern(ref.type));
breadth.push(visitor.clone());
}
});
Expand All @@ -276,7 +276,7 @@ function registerPathToRoot(refs, registry) {
if (edgeName) {
pathName = edgeName + ': ' + pathName;
}
edgeRef.rootPath = registry.insert(ref.rootPath, pathName);
edgeRef.rootPath = registry.insert(ref.rootPath, strings.intern(pathName));
nextBreadth.push(edgeVisitor.clone());
// copy module and react tree forward
if (edgeRef.module === undefined) {
Expand Down Expand Up @@ -330,10 +330,10 @@ function captureRegistry() {
let dataOffset = this.data.length;
this.data = null;

registerPathToRoot(capture.refs, this.stacks);
registerPathToRoot(capture.refs, this.stacks, this.strings);
const internedCaptureId = this.strings.intern(captureId);
const noneString = this.strings.intern('#none');
const noneStack = this.stacks.insert(this.stacks.root, '#none');
const noneStack = this.stacks.insert(this.stacks.root, noneString);
forEachRef(capture.refs, (visitor) => {
const ref = visitor.getRef();
const id = visitor.id;
Expand Down Expand Up @@ -382,7 +382,7 @@ function captureRegistry() {
const agStacks = this.stacks.flatten();
const agData = this.data;
const agNumRows = agData.length / numFields;
const ag = new aggrow(agStrings, agStacks, agNumRows);
const ag = new aggrow(agNumRows);

ag.addFieldExpander('Id',
function getId(row) {
Expand Down Expand Up @@ -414,11 +414,19 @@ function captureRegistry() {
return agData[rowA * numFields + traceField] - agData[rowB * numFields + traceField];
});

const pathExpander = ag.addCalleeStackExpander('Path',
function getStack(row) { return agStacks.get(agData[row * numFields + pathField]); });
const pathExpander = ag.addCalleeStackExpander(
'Path',
agStacks.maxDepth,
function getStack(row) { return agStacks.get(agData[row * numFields + pathField]); },
function getFrame(id) { return agStrings.get(id); },
);

const reactExpander = ag.addCalleeStackExpander('React Tree',
function getStack(row) { return agStacks.get(agData[row * numFields + reactField]); });
const reactExpander = ag.addCalleeStackExpander(
'React Tree',
agStacks.maxDepth,
function getStack(row) { return agStacks.get(agData[row * numFields + reactField]); },
function getFrame(id) { return agStrings.get(id); },
);

const valueExpander = ag.addFieldExpander('Value',
function getValue(row) { return agStrings.get(agData[row * numFields + valueField]); },
Expand Down
2 changes: 1 addition & 1 deletion local-cli/server/middleware/heapCaptureMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function symbolicateHeapCaptureFunctions(capture, onFailure, onSuccess) {
if (original.name) {
ref.value.name = original.name;
} else if (!ref.value.name) {
ref.value.name = path.posix.basename(original.source) + ':' + original.line;
ref.value.name = path.posix.basename(original.source || '') + ':' + original.line;
}
ref.value.url = original.source;
ref.value.line = original.line;
Expand Down

0 comments on commit 217a444

Please sign in to comment.