@@ -60,7 +60,7 @@ def track_for(self, pid: Any, tid: Any) -> ChromeTrack:
6060 return ChromeTrack (pid = pid , tid = tid , name = _clean_track_name (pid , tid ))
6161
6262
63- @dataclass (frozen = True , slots = True )
63+ @dataclass (slots = True )
6464class DurationSlice :
6565 """A Chrome JSON ``ph='X'`` duration event with normalized timing.
6666
@@ -458,20 +458,6 @@ def _flow_id(value: Any) -> int | None:
458458 return None
459459
460460
461- def _slice_correlation_id (slc : DurationSlice ) -> int | None :
462- return _flow_id (_event_args (slc .event ).get ("correlation" ))
463-
464-
465- def _is_cuda_launch_slice (slc : DurationSlice ) -> bool :
466- name = str (slc .event .get ("name" , "" ))
467- return name .startswith (("cudaLaunch" , "cuLaunch" )) or "LaunchKernel" in name
468-
469-
470- def _is_gpu_kernel_slice (slc : DurationSlice ) -> bool :
471- args = _event_args (slc .event )
472- return "stream" in args and "device" in args and not _is_cuda_launch_slice (slc )
473-
474-
475461def _smallest_containing_slice (
476462 slices : list [DurationSlice ],
477463 flow : FlowInstant ,
@@ -503,23 +489,35 @@ def _paired_flow_ids_by_slice(
503489 if flow_id is not None :
504490 flows_by_id [flow_id ].append (flow )
505491
506- slices_by_correlation : dict [int , list [DurationSlice ]] = defaultdict (list )
492+ launch_slices_by_correlation : dict [int , list [DurationSlice ]] = defaultdict (list )
493+ kernel_slices_by_correlation : dict [int , list [DurationSlice ]] = defaultdict (list )
507494 for slc in slices :
508- correlation_id = _slice_correlation_id (slc )
509- if correlation_id is not None :
510- slices_by_correlation [correlation_id ].append (slc )
495+ args = _event_args (slc .event )
496+ correlation_id = _flow_id (args .get ("correlation" ))
497+ if correlation_id is None :
498+ continue
499+ name = str (slc .event .get ("name" , "" ))
500+ if name .startswith (("cudaLaunch" , "cuLaunch" )) or "LaunchKernel" in name :
501+ launch_slices_by_correlation [correlation_id ].append (slc )
502+ elif "stream" in args and "device" in args :
503+ kernel_slices_by_correlation [correlation_id ].append (slc )
511504
512505 flow_ids_by_slice : dict [int , set [int ]] = defaultdict (set )
513506 latencies_by_slice : dict [int , dict [int , float ]] = defaultdict (dict )
514507 for flow_id , markers in flows_by_id .items ():
515- sources = [marker for marker in markers if marker .event .get ("ph" ) in {"s" , "t" }]
516- destinations = [marker for marker in markers if marker .event .get ("ph" ) == "f" ]
508+ sources : list [FlowInstant ] = []
509+ destinations : list [FlowInstant ] = []
510+ for marker in markers :
511+ ph = marker .event .get ("ph" )
512+ if ph in {"s" , "t" }:
513+ sources .append (marker )
514+ elif ph == "f" :
515+ destinations .append (marker )
517516 if not sources or not destinations :
518517 continue
519518
520- correlated_slices = slices_by_correlation .get (flow_id , [])
521- launch_slices = [slc for slc in correlated_slices if _is_cuda_launch_slice (slc )]
522- kernel_slices = [slc for slc in correlated_slices if _is_gpu_kernel_slice (slc )]
519+ launch_slices = launch_slices_by_correlation .get (flow_id , [])
520+ kernel_slices = kernel_slices_by_correlation .get (flow_id , [])
523521
524522 source = min (sources , key = lambda marker : marker .ts_us )
525523 source_slice = min (launch_slices , key = lambda slc : slc .ts_us ) if launch_slices else None
@@ -575,21 +573,17 @@ def assign_trackevent_lanes(
575573 else :
576574 flow_ids_by_slice , latencies_by_slice = {}, {}
577575
576+ # Mutate the internal slice models in place instead of allocating a second
577+ # DurationSlice for every Chrome ``X`` event. These objects are converter
578+ # internals and are not reused after assignment in the public write path.
579+ for slc in parsed .duration_slices :
580+ slc .lane = lane_by_index [slc .index ]
581+ slc .flow_ids = tuple (sorted (flow_ids_by_slice .get (slc .index , ())))
582+ slc .flow_latencies_us = tuple (sorted (latencies_by_slice .get (slc .index , {}).items ()))
583+
578584 return AssignedTrace (
579585 metadata = parsed .metadata ,
580- duration_slices = [
581- DurationSlice (
582- event = slc .event ,
583- index = slc .index ,
584- track = slc .track ,
585- ts_us = slc .ts_us ,
586- dur_us = slc .dur_us ,
587- lane = lane_by_index [slc .index ],
588- flow_ids = tuple (sorted (flow_ids_by_slice .get (slc .index , ()))),
589- flow_latencies_us = tuple (sorted (latencies_by_slice .get (slc .index , {}).items ())),
590- )
591- for slc in parsed .duration_slices
592- ],
586+ duration_slices = parsed .duration_slices ,
593587 instants = parsed .instants ,
594588 counters = parsed .counters ,
595589 flows = parsed .flows ,
0 commit comments