Trace View: Fix issue with critical path performance (#77373)

Store child spans in Map for faster access on critical path render
This commit is contained in:
Andre Pereira 2023-10-30 14:26:33 +00:00 committed by GitHub
parent 06873f1be3
commit 41d394c594
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -211,9 +211,23 @@ function generateRowStatesFromTrace(
: [];
}
function childSpansMap(trace: Trace | TNil) {
const childSpansMap = new Map<string, string[]>();
if (!trace) {
return childSpansMap;
}
trace.spans.forEach((span) => {
if (span.childSpanIds.length) {
childSpansMap.set(span.spanID, span.childSpanIds);
}
});
return childSpansMap;
}
const memoizedGenerateRowStates = memoizeOne(generateRowStatesFromTrace);
const memoizedViewBoundsFunc = memoizeOne(createViewedBoundsFunc, isEqual);
const memoizedGetClipping = memoizeOne(getClipping, isEqual);
const memoizedChildSpansMap = memoizeOne(childSpansMap);
// export from tests
export class UnthemedVirtualizedTraceView extends React.Component<VirtualizedTraceViewProps> {
@ -290,6 +304,10 @@ export class UnthemedVirtualizedTraceView extends React.Component<VirtualizedTra
});
}
getChildSpansMap() {
return memoizedChildSpansMap(this.props.trace);
}
getAccessors() {
const lv = this.listView;
if (!lv) {
@ -461,10 +479,10 @@ export class UnthemedVirtualizedTraceView extends React.Component<VirtualizedTra
// This function called recursively to find all descendants of a span
const findAllDescendants = (currentChildSpanIds: string[]) => {
currentChildSpanIds.forEach((eachId) => {
const currentChildSpan = trace.spans.find((a) => a.spanID === eachId)!;
if (currentChildSpan.hasChildren) {
allChildSpanIds.push(...currentChildSpan.childSpanIds);
findAllDescendants(currentChildSpan.childSpanIds);
const childrenOfCurrent = this.getChildSpansMap().get(eachId);
if (childrenOfCurrent?.length) {
allChildSpanIds.push(...childrenOfCurrent);
findAllDescendants(childrenOfCurrent);
}
});
};