From b26ebeb807e269abc04d6ea30d8ed4c847e9bf53 Mon Sep 17 00:00:00 2001 From: Lloyd Date: Mon, 20 Apr 2026 16:04:19 +0100 Subject: [PATCH] fix: optimize memory tracing by reducing overhead and filtering snapshots --- repeater/web/api_endpoints.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/repeater/web/api_endpoints.py b/repeater/web/api_endpoints.py index 16fdeef..ff65b3b 100644 --- a/repeater/web/api_endpoints.py +++ b/repeater/web/api_endpoints.py @@ -1220,8 +1220,12 @@ class APIEndpoints: if action == "start": if not tracemalloc.is_tracing(): - tracemalloc.start(10) - self._tracemalloc_baseline = tracemalloc.take_snapshot() + # Use 1 frame instead of 10 — much less overhead & faster snapshots + tracemalloc.start(1) + self._tracemalloc_baseline = tracemalloc.take_snapshot().filter_traces(( + tracemalloc.Filter(False, tracemalloc.__file__), + tracemalloc.Filter(False, ""), + )) logger.info("Memory tracing started") return self._success({ "tracing": True, @@ -1239,7 +1243,7 @@ class APIEndpoints: # ---------- GET: status + data ---------- tracing = tracemalloc.is_tracing() - result = {"tracing": tracing} + result: dict = {"tracing": tracing} # Always include RSS regardless of tracing state try: @@ -1252,7 +1256,11 @@ class APIEndpoints: if not tracing: return self._success(result) - current = tracemalloc.take_snapshot() + # Filter out tracemalloc's own allocations to keep snapshot small & fast + current = tracemalloc.take_snapshot().filter_traces(( + tracemalloc.Filter(False, tracemalloc.__file__), + tracemalloc.Filter(False, ""), + )) baseline = getattr(self, "_tracemalloc_baseline", None) # Top 20 allocations right now