Skip to content

screenshot-detector: only name the processes that changed between polls - #12

Merged
Rrrima merged 1 commit into
mainfrom
perf/incremental-process-scan
Nov 24, 2026
Merged

Rrrima merged 1 commit into
mainfrom
perf/incremental-process-scan

Conversation

@Rrrima

@Rrrima Rrrima commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

why

the monitor scans the process table every 30 ms, and each scan asked every process for its name. on a mac with about 600 processes that is roughly 20,000 system calls a second: about 6% of a cpu core, all the time, in every app that embeds ScreenshotMonitorKit.

what

ProcessScanner gives the same answer as ScreenshotProcessScan.scan() but only names:

  • pids it has not seen before
  • pids first seen less than a second ago (a new process carries its parent's name until it execs)
  • everything, once every 2 seconds, as a safety net

the poll interval is unchanged, so a new screencapture is still seen on the next poll.

one case is slower: a process more than a second old that replaces itself with screencapture is noticed within 2 seconds instead of 30 ms.

measured

sampled inside an app that embeds the kit, idle: the monitor queue went from 6.3% of a core to 0.9%.

tests

  • 8 new unit tests for the scanner, one comparing it with a plain scan of the real process table
  • ran ScreenshotHelper from this branch and took three screencapture -x shots: all three detected and captured

The monitor scans the process table every 30 ms and asked every process
for its name each time: about 20,000 system calls a second and 6% of a
CPU core on a Mac with 600 processes, around the clock.

The scan now lists the pids (one call) and names only new ones, ones
under a second old (a process takes its own name a moment after it is
created), and everything again every 2 seconds. Same poll interval, same
result; the scan costs about 0.9% of a core.
@Rrrima
Rrrima merged commit 85a0a92 into main Nov 24, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant