Skip to content

Commit

Permalink
Avoid repeatedly re-parsing frames
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sparrowt authored Nov 14, 2023
1 parent 8444d17 commit 2bca0d6
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions austin/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
]
Expand Down

0 comments on commit 2bca0d6

Please sign in to comment.