fix(bench): del ctypes/bytes objects immediately in callback to prevent OOM on 1GB device

This commit is contained in:
Matteo Benedetto
2026-03-24 00:53:03 +01:00
parent a201594a90
commit 3e8661e2e5
+11 -8
View File
@@ -183,20 +183,23 @@ def _on_sample(sink) -> Gst.FlowReturn:
except Exception:
pass
# Measure from_buffer_copy() cost (the CPU copy the app does)
# Measure extract_dup (GStreamer buf → Python bytes) + from_buffer_copy
# (Python bytes → ctypes array for SDL upload). del objects immediately
# after timing so CPython's ref-counting frees the 3+ MB allocations at
# once rather than letting them accumulate across frames (OOM on 1 GB device).
t0 = time.monotonic()
raw = buf.extract_dup(0, buf.get_size())
copy_us = (time.monotonic() - t0) * 1e6
extract_us = (time.monotonic() - t0) * 1e6
if fmt_str == "NV12":
y_size = int(info.stride[0]) * int(info.height)
# Simulate the single from_buffer_copy the app now does:
# ONE copy of the full Y+UV buffer, then ctypes.byref for the UV offset.
y_size = int(info.stride[0]) * int(info.height)
t1 = time.monotonic()
arr = (ctypes.c_ubyte * len(raw)).from_buffer_copy(raw)
y_ptr = ctypes.cast(arr, ctypes.POINTER(ctypes.c_ubyte))
uv_ptr = ctypes.cast(ctypes.byref(arr, y_size), ctypes.POINTER(ctypes.c_ubyte))
copy_us = (time.monotonic() - t1) * 1e6
copy_us = extract_us + (time.monotonic() - t1) * 1e6
del arr # free 3 MB ctypes array immediately
else:
copy_us = extract_us
del raw # free 3 MB bytes object immediately
with stats.lock:
stats.total_frames += 1