Skip to content

Commit e782314

Browse files
authored
Rollup merge of #106216 - ChrisDenton:ps-go-faster, r=jyn514
Powershell: Use `WaitForExit` instead of `-Wait` Using the method `WaitForExit` instead of the parameter `-Wait` results in a notable speed up of the `x.ps1` script (~350ms, fairly consistently). Results: ``` milliseconds before: 1127.7576 milliseconds after: 779.0467 ``` I think there are opportunities for further speed up by calling `Get-Command` only once with the pattern `py*` then filtering the returned list. But I'll leave that for another time (or someone else). r? ``@jyn514``
2 parents 497d214 + 96501bd commit e782314

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Diff for: x.ps1

+8-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ function Get-Application($app) {
1414
return Get-Command $app -ErrorAction SilentlyContinue -CommandType Application
1515
}
1616

17+
function Invoke-Application($application, $arguments) {
18+
$process = Start-Process -NoNewWindow -PassThru $application $arguments
19+
$process.WaitForExit()
20+
Exit $process.ExitCode
21+
}
22+
1723
foreach ($python in "py", "python3", "python", "python2") {
1824
# NOTE: this only tests that the command exists in PATH, not that it's actually
1925
# executable. The latter is not possible in a portable way, see
@@ -23,16 +29,14 @@ foreach ($python in "py", "python3", "python", "python2") {
2329
# Use python3, not python2
2430
$xpy_args = @("-3") + $xpy_args
2531
}
26-
$process = Start-Process -NoNewWindow -Wait -PassThru $python $xpy_args
27-
Exit $process.ExitCode
32+
Invoke-Application $python $xpy_args
2833
}
2934
}
3035

3136
$found = (Get-Application "python*" | Where-Object {$_.name -match '^python[2-3]\.[0-9]+(\.exe)?$'})
3237
if (($null -ne $found) -and ($found.Length -ge 1)) {
3338
$python = $found[0]
34-
$process = Start-Process -NoNewWindow -Wait -PassThru $python $xpy_args
35-
Exit $process.ExitCode
39+
Invoke-Application $python $xpy_args
3640
}
3741

3842
Write-Error "${PSCommandPath}: error: did not find python installed"

0 commit comments

Comments
 (0)