Skip to content

Commit 6a0df1f

Browse files
Improve performance of expanding and collapsing spans
Use Set and Map to make lookups faster. This changes makes collapsing and expanding spans with large (10k) amount of children 200-500ms faster. Signed-off-by: Damian Maslanka <[email protected]>
1 parent 9ba711a commit 6a0df1f

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

packages/jaeger-ui/src/components/TracePage/TraceTimelineViewer/VirtualizedTraceView.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ function getCssClasses(currentViewRange: [number, number]) {
159159
});
160160
}
161161

162+
const memoizedSpanByID = memoizeOne((spans: Span[]) => new Map(spans.map(x => [x.spanID, x])));
163+
162164
function mergeChildrenCriticalPath(
163165
trace: Trace,
164166
spanID: string,
@@ -168,23 +170,23 @@ function mergeChildrenCriticalPath(
168170
return [];
169171
}
170172
// Define an array to store the IDs of the span and its descendants (if the span is collapsed)
171-
const allRequiredSpanIds = [spanID];
173+
const allRequiredSpanIds = new Set<string>([spanID]);
172174

173175
// If the span is collapsed, recursively find all of its descendants.
174-
const findAllDescendants = (currentChildSpanIds: string[]) => {
176+
const findAllDescendants = (currentChildSpanIds: Set<string>) => {
175177
currentChildSpanIds.forEach(eachId => {
176-
const currentChildSpan = trace.spans.find(a => a.spanID === eachId)!;
178+
const currentChildSpan = memoizedSpanByID(trace.spans).get(eachId)!;
177179
if (currentChildSpan.hasChildren) {
178-
allRequiredSpanIds.push(...currentChildSpan.childSpanIds);
179-
findAllDescendants(currentChildSpan.childSpanIds);
180+
currentChildSpan.childSpanIds.forEach(x => allRequiredSpanIds.add(x));
181+
findAllDescendants(new Set<string>(currentChildSpan.childSpanIds));
180182
}
181183
});
182184
};
183185
findAllDescendants(allRequiredSpanIds);
184186

185187
const criticalPathSections: criticalPathSection[] = [];
186188
criticalPath.forEach(each => {
187-
if (allRequiredSpanIds.includes(each.spanId)) {
189+
if (allRequiredSpanIds.has(each.spanId)) {
188190
if (criticalPathSections.length !== 0 && each.section_end === criticalPathSections[0].section_start) {
189191
// Merge Critical Paths if they are consecutive
190192
criticalPathSections[0].section_start = each.section_start;

0 commit comments

Comments
 (0)