From 15efbf5df3b8d116736563843bdcace5e4cca561 Mon Sep 17 00:00:00 2001 From: Tom Sparrow <793763+sparrowt@users.noreply.github.com> Date: Mon, 20 Nov 2023 12:50:56 +0000 Subject: [PATCH] perf: avoid repeatedly re-parsing frames (#27) * 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. * Avoid changing the type of existing variable `frames` * Code formatting * Moar black --------- Co-authored-by: Gabriele N. Tornetta --- austin/stats.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/austin/stats.py b/austin/stats.py index cf7f2f0..ca71d68 100644 --- a/austin/stats.py +++ b/austin/stats.py @@ -149,9 +149,13 @@ def parse(metrics: str, metric_type: Optional[MetricType] = None) -> List["Metri ms = [int(_) for _ in metrics.split(",")] if len(ms) == 3: return [ + # CPU time Metric(MetricType.TIME, ms[0] if ms[1] == 0 else 0), + # Wall time Metric(MetricType.TIME, ms[0]), + # Memory allocation Metric(MetricType.MEMORY, ms[2] if ms[2] >= 0 else 0), + # Memory deallocation Metric(MetricType.MEMORY, -ms[2] if ms[2] < 0 else 0), ] elif len(ms) != 1: @@ -260,14 +264,15 @@ def parse(sample: str, metric_type: Optional[MetricType] = None) -> List["Sample try: ms = Metric.parse(metrics, metric_type) + frames_parsed = ( + [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_parsed, ) for metric in ms ]