Optimize TraceQueryService.sortSpans from O(N^2) to O(N)#13831
Merged
wu-sheng merged 1 commit intoapache:masterfrom Apr 20, 2026
Merged
Optimize TraceQueryService.sortSpans from O(N^2) to O(N)#13831wu-sheng merged 1 commit intoapache:masterfrom
wu-sheng merged 1 commit intoapache:masterfrom
Conversation
findRoot and findChildren used nested linear scans to match segmentParentSpanId against segmentSpanId, producing O(N^2) behavior that is unnecessary for trace detail rendering. Pre-index the spans once per trace: - Set<String> of segmentSpanIds for the has-parent check - Map<String, List<Span>> from segmentParentSpanId to children for traversal sortSpans now runs in O(N). findRoot/findChildren/sortSpans are package-private static helpers so they can be unit tested in isolation. Add TraceQueryServiceTest covering linear chains, sibling subtrees, multi-root ordering by startTime, cross-segment parent references, and orphaned spans.
Member
|
Thanks! This is good, just asking, did you face any query performance issues? Usually, the query is paging, so, it should not have too many traces/spans. This optimization is solid, but may not be felt by human. |
Contributor
Author
|
Thanks! I didn’t see a concrete production issue yet. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TraceQueryService.sortSpans()builds the parent-child span tree for trace detail rendering. The existing implementation runs in O(N^2):findRoot()iterates all N spans and, for each, linearly scans the full list to check whether its parent exists.findChildren()is called per root and recursively scans the full list at each node to find direct children.For traces with deep call chains or many parallel spans this becomes noticeable latency on every trace detail query (a UI hot path).
This PR pre-indexes the span list once per trace and turns the algorithm into O(N):
Set<String> segmentSpanIds— membership check used byfindRootto decide if a span has a parent in the current list.Map<String, List<Span>> childrenByParentSegmentSpanId— direct lookup forfindChildren.The three helpers (
sortSpans,findRoot,findChildren) are now package-privatestaticbecause they have always been pure functions — no instance state. This also makes them directly unit-testable.Behavior
Preserved from the previous implementation:
startTime.segmentSpanIdnamespace (segmentId + 'S' + spanId).Tests
Adds
TraceQueryServiceTest(8 cases):All pass locally (
./mvnw -pl oap-server/server-core -am test -Dtest=TraceQueryServiceTest).Which issue does this PR relate to?
N/A — found by code review while looking at query hot paths.