Repository navigation
Description
Summary
getRawWindows runs Add-Type with the WinShim C# source in a new powershell.exe on every call, and prepareCapture calls it once or twice per window capture, with restore and pin adding more Add-Type runs. Each one costs the C# compile again.
Where
src/utils/windows.ts:270-326src/utils/resolveTarget.ts:104-118src/utils/windowControl.ts:28src/utils/windowControl.ts:170-171src/utils/windowControl.ts:196
Details
getRawWindows() runs PS_ENUM through runPowerShell on each call ("Not cached: windows move and restack between calls"). PS_ENUM starts with Add-Type @"...public class WinShim{...}"@, so each call starts powershell.exe and runs the C# compiler again. prepareCapture calls it once per screenshot, wait_for_change, find_text, or start_recording on a window: target, and twice when the window was minimized (resolveTarget.ts:104 and :118, plus restoreWindow, which compiles SHIM again). Covered windows add pinWindow/unpin, each another Add-Type. Inside the script there is also one Get-Process -Id per distinct owning process (windows.ts:313). #88 measured this round trip at about 1-1.5 s and fixed only the blocking (async) and the monitor-geometry cache. Its suggested "persistent helper / prebuilt helper" was never done for windows, which are the most common agent target.
Impact
A screenshot-act-screenshot agent loop on window:<title> pays about 1-1.5 s of PowerShell start-up and compile before ffmpeg starts, on every step, and 2-4x that for minimized or covered windows. Over a 50-step session that adds a minute or more of pure overhead and CPU spikes (csc.exe) that can show up in the recording itself.
Steps to reproduce
Call screenshot { target: "window:Notepad" } several times in a row. Each call spawns powershell -EncodedCommand with Add-Type (visible as csc.exe/cvtres.exe in Process Monitor), and no call gets faster after the first.
Suggested fix
Keep the per-call enumeration (window positions must be fresh) but drop the compile. Compile WinShim (and the windowControl SHIM) once with Add-Type -OutputAssembly <SCREENCAST_HOME>/cache/winshim-<hash of source>.dll, then use Add-Type -Path on later calls, which loads in milliseconds. Alternatively, keep one long-lived PowerShell worker reading requests on stdin, as the event logger and perf sampler already do. Replace the per-process Get-Process with a single Get-Process snapshot hashed by Id.
Acceptance criteria
- The shim is compiled once (cached assembly or the bundled helper) and reused across calls
- Window enumeration time per call is measured before and after
Getting started
- Relevant files:
src/utils/windows.ts,src/utils/resolveTarget.ts,src/utils/windowControl.ts - Run tests:
npm test(alsonpm run typecheckandnpm run lint); see CONTRIBUTING.md - If you change a tool's schema or description, run
npm run docs
Filed by repo-audit/full-audit at f0e5ebd. Severity: medium. Effort: M. audit-key: performance|src/utils/windows.ts|getRawWindows
Activity