Repository navigation
perf: start Windows PowerShell once, directly, to read the clipboard - #119
Merged
Merged
Conversation
On Windows a paste waited for PowerShell longer than it needed to, in three ways. PowerShell 7 was preferred when it was installed, although it takes longer to start than Windows PowerShell and the script needs nothing it adds. The command went through cmd.exe. And the first paste in a window started a PowerShell an extra time, only to see that one runs. The provider now starts powershell.exe itself, with an argument array, and lets the read be the check: the first executable that gets through the script is remembered for later reads. PowerShell 7 is tried when Windows PowerShell cannot be started or fails, so a machine that relied on it keeps working. isAvailable() answers from the platform alone. A read that outlives the timeout is reported as a timeout and does not go on to the other executable; the old check looked for an error code that a timeout does not set. macOS and Linux are untouched. Measured with the script as shipped, eight runs each, median. On an ARM64 laptop with PowerShell 7 installed the read went from 682 ms to 337 ms. On an x64 hosted CI runner it went from 465 ms to 305 ms with text on the clipboard and from 527 ms to 362 ms with an image, and the extra start that is gone took 422 ms. The new test file replaces child_process and fails on the code before this change, 11 tests of 11. One more test, on Windows only, starts the real powershell.exe with the provider's arguments. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On Windows, reading the clipboard now starts Windows PowerShell once, directly. With PowerShell 7 installed a paste waits noticeably less for it, and the first paste in a window no longer pays for a second start.
Why
Most of the time between the paste key and the path appearing is PowerShell starting. Three things made that longer than it has to be:
cmd.exe.execstarts a shell to parse a command line that has nothing for a shell to do.Change
powershell.exeis tried first andpwshsecond. The first that gets through the script is remembered for later reads. A failure of the first moves on to the second, so a machine where only PowerShell 7 works keeps working. A timeout does not move on: waiting out a second one would only make the wait longer.isAvailable()answers from the platform, and the read reports a machine where neither is found: "PowerShell could not be started".execFileand an argument array, so there is no shell and no command string.The macOS and Linux providers are untouched.
Verification
Verified
The steps of
npm run verifyon Windows ARM64: lint, both type checks, 581 unit and integration tests with the coverage thresholds, and the 13 end-to-end tests in VS Code 1.145.2.The new test file replaces
child_processand checks how PowerShell is started: which executable, the argument array, one start for the first paste, the fallback, the timeout. All 11 tests fail on the code before this change.One more test, on Windows only, starts the real
powershell.exewith the provider's arguments and expects the script's result line.The win32-arm64 VSIX built from this branch, in an isolated VS Code 1.145.2 on Windows 11 ARM64 with PowerShell 7 installed and a 1786×796 image on the real clipboard. Pasting into a Markdown editor saves the PNG and inserts the link. Pasting with the terminal focused types the path and does not run it. With text on the clipboard nothing is saved and the text is pasted. The same steps on 0.10.0 from the Marketplace give the same results.
The time of the clipboard read as the extension logs it, in that setup, with this branch and 0.10.0 in alternating windows, two windows of six pastes each:
Outside VS Code, the shipped script started the way 0.10.0 starts it and the way this branch starts it, eight runs each, median, on an x64 GitHub-hosted Windows runner with PowerShell 7 installed: 465 ms to 305 ms with text on the clipboard, 527 ms to 362 ms with an image.
Not checked
child_process, andpwshwas started with the same argument array outside VS Code. No machine without a working Windows PowerShell was used.🤖 Generated with Claude Code