Skip to content

Every window: target recompiles a C# shim in a fresh PowerShell (1–1.5 s per call) #393

Backlog

Description

@TMHSDigital

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-326
  • src/utils/resolveTarget.ts:104-118
  • src/utils/windowControl.ts:28
  • src/utils/windowControl.ts:170-171
  • src/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 (also npm run typecheck and npm 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

  1. added
    help wanted
    performance
    effort: medium
    on Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort: mediumhelp wantedperformancepriority: medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions