Optimize TraceQueryService.sortSpans from O(N^2) to O(N) (#13831) `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 by `findRoot` to decide if a span has a parent in the current list. - `Map<String, List<Span>> childrenByParentSegmentSpanId` — direct lookup for `findChildren`. The three helpers (`sortSpans`, `findRoot`, `findChildren`) are now package-private `static` because they have always been pure functions — no instance state. This also makes them directly unit-testable. ## Behavior Preserved from the previous implementation: - Root spans sorted by `startTime`. - Children traversed in input order (DFS). - Orphaned spans (parent not in the list due to lost/sampled segments) treated as roots. - Cross-segment ref parents resolved via the shared `segmentSpanId` namespace (`segmentId + 'S' + spanId`).
SkyWalking: an APM (Application Performance Monitoring) system, especially designed for microservices, cloud native and container-based architectures.
SkyWalking is an open-source APM system that provides monitoring, tracing and diagnosing capabilities for distributed systems in Cloud Native architectures.
Please head to the releases page to download a release of Apache SkyWalking.
Follow this document.
This project adheres to the Contributor Covenant code of conduct. By participating, you are expected to uphold this code. Please follow the REPORTING GUIDELINES to report unacceptable behavior.
dev-subscribe@skywalking.apache.org, follow the reply to subscribe the mail list.Request to join SkyWalking slack mail to the mail list(dev@skywalking.apache.org), we will invite you in.[CN] Request to join SkyWalking slack mail to the mail list(dev@skywalking.apache.org), we will invite you in.Hundreds of companies and organizations use SkyWalking for research, production, and commercial purposes. Visit our website to find the user page.