Skip to content

Commit d8fc3d9

Browse files
committed
Improve TrackEvent lane assignment scaling
1 parent 8197e9c commit d8fc3d9

1 file changed

Lines changed: 17 additions & 15 deletions

File tree

transformer_nuggets/utils/track_event.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -411,28 +411,30 @@ def parse_chrome_trace(trace: TraceDict) -> ParsedChromeTrace:
411411
)
412412

413413

414-
def _slices_are_nesting_compatible(a: DurationSlice, b: DurationSlice) -> bool:
415-
if a.end_us <= b.ts_us or b.end_us <= a.ts_us:
416-
return True
417-
a_contains_b = a.ts_us <= b.ts_us and a.end_us >= b.end_us
418-
b_contains_a = b.ts_us <= a.ts_us and b.end_us >= a.end_us
419-
return a_contains_b or b_contains_a
420-
421-
422414
def _assign_nesting_lanes(slices: list[DurationSlice]) -> dict[int, int]:
423-
"""Assign slices to lanes where each lane can be emitted as nested TrackEvents."""
424-
lanes: list[list[DurationSlice]] = []
415+
"""Assign slices to lanes where each lane can be emitted as nested TrackEvents.
416+
417+
TrackEvent begin/end packets support properly nested slices on one track,
418+
but not crossing intervals. Since slices are processed by start time with
419+
longer equal-start slices first, a lane is valid when the new slice either
420+
starts after the active stack or is contained by the current innermost
421+
active slice. This keeps assignment close to O(number of slices * lanes)
422+
instead of checking every earlier slice in the lane.
423+
"""
424+
lane_end_stacks: list[list[float]] = []
425425
assignments: dict[int, int] = {}
426426

427427
for slc in sorted(slices, key=lambda s: (s.ts_us, -s.end_us, s.index)):
428-
for lane_idx, lane_slices in enumerate(lanes):
429-
if all(_slices_are_nesting_compatible(slc, other) for other in lane_slices):
430-
lane_slices.append(slc)
428+
for lane_idx, end_stack in enumerate(lane_end_stacks):
429+
while end_stack and end_stack[-1] <= slc.ts_us:
430+
end_stack.pop()
431+
if not end_stack or slc.end_us <= end_stack[-1]:
432+
end_stack.append(slc.end_us)
431433
assignments[slc.index] = lane_idx
432434
break
433435
else:
434-
assignments[slc.index] = len(lanes)
435-
lanes.append([slc])
436+
assignments[slc.index] = len(lane_end_stacks)
437+
lane_end_stacks.append([slc.end_us])
436438

437439
return assignments
438440

0 commit comments

Comments
 (0)