Skip to content

perf(process_manager): batch process enumeration, cache SID lookup#198

Open
HetCreep wants to merge 1 commit into
fa0311:masterfrom
HetCreep:perf/process-enumeration
Open

perf(process_manager): batch process enumeration, cache SID lookup#198
HetCreep wants to merge 1 commit into
fa0311:masterfrom
HetCreep:perf/process-enumeration

Conversation

@HetCreep

@HetCreep HetCreep commented Jun 1, 2026

Copy link
Copy Markdown

Summary

Two small, behaviour-preserving performance tweaks in lib/process_manager.py.

1. Batch process enumeration

ProcessIdManager.__init__ called x.exe() for every process from process_iter() - one syscall per process. Since new_process() builds a fresh snapshot before and after each launch to diff them, this runs on the full process table repeatedly.

process_iter(attrs=["pid", "exe"]) fetches both fields in a single pass and stores None for exe when access is denied - the same result the old try/except wrapper produced - without the per-process call. This is the pattern the psutil docs recommend.

2. Cache the SID lookup

get_sid() performs a win32 LookupAccountName + ConvertSidToStringSid on every call, but the current user's SID is constant for the process lifetime. functools.lru_cache(maxsize=1) makes repeated Schtasks operations reuse the first result.

Notes

No behavioural change; output is identical. Net +8 / -7.

ProcessIdManager built its snapshot by calling x.exe() on every process
returned by process_iter() — one syscall per process, on a list rebuilt
each time new_process() diffs before/after a launch. Switch to
process_iter(attrs=["pid", "exe"]), which fetches both fields in one pass
and yields None for exe on AccessDenied (same result as the old try/except
wrapper), markedly faster on machines with many processes.

Also cache get_sid() with lru_cache(maxsize=1): the current user's SID is
constant for the process lifetime, so the win32 lookup need not repeat on
every scheduled-task operation.

No behavioural change.
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