Files
hyprland-headless-stream-pl…/spikes/capture-speed/copyout_probe.py
T
Hermenix e2d9cb7dd4 spike: record what crashes the compositor, and the copy probe that explains grim-rs
CRASH-FINDING.md is the delegated session's sharper version of the crash result:
hold a live ext capture session on a session's output, kill the session, call
create_frame -> SIGSEGV in CScreenshareFrame::transform(). That is a reproducible
trigger, not a correlation, and it is why ext-image-copy-capture was measured and
then not adopted.

copyout_probe.py prices the per-frame allocate/copy/swizzle that grim-rs's API
forces: 2.2 ms of client time, which is nowhere near its 46 ms deficit against the
in-process route -- so the difference is buffer reuse, not language.
2026-10-11 22:47:28 +00:00

78 lines
2.8 KiB
Python

#!/usr/bin/env python3
"""What does a per-frame copy + swizzle cost? Isolating the grim-rs gap.
The suspicion this answers: a compiled Rust library should not be *slower* than a
subprocess, and pure Python should not be faster than either. The explanation
under test is that the routes are not doing the same work.
grim-rs's public API returns owned pixels -- per capture it does
`mmap.to_vec()` plus an XRGB/ARGB -> RGBA swizzle (its own source, and its own
notes in the results). Our producer hands the mapped buffer straight to the
encoder: no allocation, no copy, no conversion.
So this measures what that extra work costs on this box, on its own, with no
compositor involved: allocate a mapping the size of one 1280x720 frame, then time
(a) copying it out and (b) swizzling it, per frame, over 200 frames.
If the copy+swizzle costs about the gap between our number (~78 ms) and grim-rs's
(~124 ms), then the difference is the work, not the language -- and the honest
conclusion is "a client that reuses its buffer beats one that does not", not
"Python beats Rust".
"""
from __future__ import annotations
import mmap
import os
import statistics
import sys
import time
WIDTH, HEIGHT = 1280, 720
STRIDE = WIDTH * 4
SIZE = STRIDE * HEIGHT
FRAMES = 200
def main() -> int:
memory = mmap.mmap(-1, SIZE)
# Fill with something non-trivial so the copy is not optimised away.
memory.write(b"\x01\x02\x03\x04" * (SIZE // 4))
copies: list[float] = []
swizzles: list[float] = []
both: list[float] = []
for _ in range(FRAMES):
start = time.monotonic_ns()
pixels = bytes(memoryview(memory))
copies.append((time.monotonic_ns() - start) / 1e6)
start = time.monotonic_ns()
swapped = bytearray(len(pixels))
swapped[0::4] = pixels[2::4]
swapped[1::4] = pixels[1::4]
swapped[2::4] = pixels[2::4]
swapped[3::4] = pixels[3::4]
swizzles.append((time.monotonic_ns() - start) / 1e6)
both.append(copies[-1] + swizzles[-1])
del pixels, swapped
print(f"frame: {WIDTH}x{HEIGHT}, {SIZE / 1e6:.2f} MB, {FRAMES} frames")
for label, samples in (("copy only (mmap -> Vec)", copies),
("swizzle only (BGRX -> RGBA)", swizzles),
("copy + swizzle", both)):
print(f" {label:<28} p50 {statistics.median(samples):>6.2f} ms "
f"min {min(samples):>6.2f} max {max(samples):>6.2f}")
print("\nAdd 'copy + swizzle' to the in-process route's number to compare it "
"against a library that returns owned pixels.")
memory.close()
# Flush before `os._exit`: os._exit skips the interpreter's cleanup, so
# buffered stdout is dropped -- the first run of this printed nothing at all.
sys.stdout.flush()
os._exit(0)
if __name__ == "__main__":
raise SystemExit(main())