From 2bca0d6f2ec866bc9636a4d3113b9673b69a1092 Mon Sep 17 00:00:00 2001 From: Tom Sparrow <793763+sparrowt@users.noreply.github.com> Date: Tue, 14 Nov 2023 11:24:11 +0000 Subject: [PATCH] Avoid repeatedly re-parsing frames This should reduce the effort on parsing frames by a factor of 4 when operating on a `--full` trace. In that case `ms` will be a list of 4 metrics but we only need to parse the frames once because they are the same in all cases. Also add a couple of comments which weren't obvious to me on first reading. --- austin/stats.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/austin/stats.py b/austin/stats.py index 06cf469..9954fe2 100644 --- a/austin/stats.py +++ b/austin/stats.py @@ -149,10 +149,10 @@ def parse(metrics: str, metric_type: Optional[MetricType] = None) -> List["Metri ms = [int(_) for _ in metrics.split(",")] if len(ms) == 3: return [ - Metric(MetricType.TIME, ms[0] if ms[1] == 0 else 0), - Metric(MetricType.TIME, ms[0]), - Metric(MetricType.MEMORY, ms[2] if ms[2] >= 0 else 0), - Metric(MetricType.MEMORY, -ms[2] if ms[2] < 0 else 0), + Metric(MetricType.TIME, ms[0] if ms[1] == 0 else 0), # cpu time + Metric(MetricType.TIME, ms[0]), # wall time + Metric(MetricType.MEMORY, ms[2] if ms[2] >= 0 else 0), # memory allocation + Metric(MetricType.MEMORY, -ms[2] if ms[2] < 0 else 0), # memory deallocation ] elif len(ms) != 1: raise ValueError() @@ -260,14 +260,13 @@ def parse(sample: str, metric_type: Optional[MetricType] = None) -> List["Sample try: ms = Metric.parse(metrics, metric_type) + frames = [Frame.parse(frame) for frame in frames.split(";")] if frames else [] return [ Sample( pid=int(pid), thread=thread, metric=metric, - frames=[Frame.parse(frame) for frame in frames.split(";")] - if frames - else [], + frames=frames, ) for metric in ms ]