Title: Speed up parameter binding by skipping mandatory parameters outside the active parameter set by blakedeckard · Pull Request #28371 · PowerShell/PowerShell · GitHub
Open Graph Title: Speed up parameter binding by skipping mandatory parameters outside the active parameter set by blakedeckard · Pull Request #28371 · PowerShell/PowerShell
X Title: Speed up parameter binding by skipping mandatory parameters outside the active parameter set by blakedeckard · Pull Request #28371 · PowerShell/PowerShell
Description: PR Summary Skip mandatory-parameter bookkeeping for parameters that are not in any of the currently valid parameter sets. For every pipeline object, CmdletParameterBinderController.GetMissingMandatoryParameters loops over each unbound parameter that is mandatory in some parameter set and enumerates its parameter set data, even when the parameter is not in any currently valid set. For commands with many parameter sets this is a large part of the per-object binding cost. Where-Object has 32 parameter sets (one per operator switch) and 32 parameters that are mandatory in one of them, so with the script block parameter set all 31 enumerations find nothing for every input object. The new check is the same one BindUnboundParametersForBindingStateInParameterSet (and several other places) already use: (ParameterSetFlags & _currentParameterSetFlag) == 0 && !IsInAllSets. Since ParameterSetFlags is the OR of all the parameter's set bits and IsInAllSets is set when any of its sets is __AllParameterSets (GenerateParameterSetMappingFromMetadata), GetMatchingParameterSetData returns nothing for such a parameter, so the result is unchanged. PR Context Measured on Linux x64, Release build with ReadyToRun. Each number is the median of 5 alternating process runs; each run takes the fastest of 5 repetitions: Command Before After Change 1..200000 | Where-Object { $_ % 2 } 691 ms 422 ms -39% $objs | Where-Object Tag -eq 3 (50k objects) 132 ms 80 ms -40% 1..200000 | ForEach-Object { $_ } 420 ms 406 ms -3% Per object, measured in-process with [GC]::GetTotalAllocatedBytes and [Diagnostics.Stopwatch]: Command Allocated before Allocated after Change Time before Time after Change 1..200000 | Where-Object { $_ % 2 } 4,464 B 2,232 B -50% 3.45 µs 2.11 µs -39% $objs | Where-Object Tag -eq 3 (50k objects) 3,144 B 912 B -71% 2.65 µs 1.59 µs -40% 1..200000 | ForEach-Object { $_ } 1,936 B 1,792 B -7% 2.10 µs 2.03 µs -3% Allocated bytes were identical in every run. Benchmark script <# .SYNOPSIS Before/after benchmark for "Skip unrelated parameters when checking for missing mandatory parameters". .DESCRIPTION Runs the Before and After pwsh builds in alternating processes (to spread out machine noise), and in each process measures every case in-process with [Diagnostics.Stopwatch] and [GC]::GetTotalAllocatedBytes. Each process reports the best of $Reps repetitions; the script then prints the median across $Rounds processes as two markdown tables. .EXAMPLE ./bench-where-object.ps1 -Before ~/ps-master/release/pwsh -After ~/ps-patched/release/pwsh (Build each with: Start-PSBuild -Configuration Release -Output release) #> param( [Parameter(Mandatory)] [string] $Before, [Parameter(Mandatory)] [string] $After, [int] $Rounds = 5, [int] $Reps = 5 ) # Code that runs inside each child pwsh process. Prints one JSON line. $worker = @' param([int] $Reps) $objs = 1..50000 | ForEach-Object { [pscustomobject]@{ Id = $_; Name = "item$_"; Tag = ($_ % 10) } } $cases = [ordered]@{ '`1..200000 | Where-Object { $_ % 2 }`' = @{ N = 200000; S = { 1..200000 | Where-Object { $_ % 2 } | Out-Null } } '`$objs | Where-Object Tag -eq 3` (50k objects)' = @{ N = 50000; S = { $objs | Where-Object Tag -eq 3 | Out-Null } } '`1..200000 | ForEach-Object { $_ }`' = @{ N = 200000; S = { 1..200000 | ForEach-Object { $_ } | Out-Null } } } $out = [ordered]@{} foreach ($name in $cases.Keys) { $c = $cases[$name] & $c.S; & $c.S # warm up (JIT, caches) $bestMs = [double]::MaxValue; $bestBytes = [long]::MaxValue for ($i = 0; $i -lt $Reps; $i++) { [GC]::Collect(); [GC]::WaitForPendingFinalizers() $a0 = [GC]::GetTotalAllocatedBytes($true) $sw = [Diagnostics.Stopwatch]::StartNew() & $c.S $sw.Stop() $a1 = [GC]::GetTotalAllocatedBytes($true) $bestMs = [Math]::Min($bestMs, $sw.Elapsed.TotalMilliseconds) $bestBytes = [Math]::Min($bestBytes, $a1 - $a0) } $out[$name] = @{ ms = $bestMs; n = $c.N; bytes = $bestBytes } } $out | ConvertTo-Json -Compress -Depth 3 '@ $workerPath = Join-Path ([IO.Path]::GetTempPath()) "bench-where-object-worker.ps1" Set-Content -Path $workerPath -Value $worker $results = @{ Before = @(); After = @() } for ($r = 1; $r -le $Rounds; $r++) { foreach ($side in 'Before', 'After') { $exe = if ($side -eq 'Before') { $Before } else { $After } Write-Host "round $r/$Rounds $side" $json = & $exe -NoProfile -NonInteractive -File $workerPath -Reps $Reps | Select-Object -Last 1 $results[$side] += , ($json | ConvertFrom-Json -AsHashtable) } } function Median([double[]] $v) { $s = $v | Sort-Object; if ($s.Count % 2) { $s[[int][Math]::Floor($s.Count / 2)] } else { ($s[$s.Count / 2 - 1] + $s[$s.Count / 2]) / 2 } } function Pct($a, $b) { '{0:+0;-0;0}%' -f (100 * ($b - $a) / $a) } $names = $results.Before[0].Keys '' "Whole pipeline, median of $Rounds alternating process runs, each the best of $Reps repetitions:" '' '| Command | Before | After | Change |' '|---|---:|---:|---:|' foreach ($n in $names) { $b = Median ($results.Before | ForEach-Object { $_[$n].ms }); $a = Median ($results.After | ForEach-Object { $_[$n].ms }) '| {0} | {1:N0} ms | {2:N0} ms | {3} |' -f ($n -replace '\|', '\|'), $b, $a, (Pct $b $a) } '' 'Per object, measured in-process with `[GC]::GetTotalAllocatedBytes` and `[Diagnostics.Stopwatch]`:' '' '| Command | Allocated before | Allocated after | Change | Time before | Time after | Change |' '|---|---:|---:|---:|---:|---:|---:|' foreach ($n in $names) { $cnt = $results.Before[0][$n].n $bb = Median ($results.Before | ForEach-Object { $_[$n].bytes / $cnt }); $ab = Median ($results.After | ForEach-Object { $_[$n].bytes / $cnt }) $bt = Median ($results.Before | ForEach-Object { $_[$n].ms * 1000 / $cnt }); $at = Median ($results.After | ForEach-Object { $_[$n].ms * 1000 / $cnt }) $bAll = $results.Before | ForEach-Object { [math]::Round($_[$n].bytes / $cnt) } | Sort-Object -Unique $aAll = $results.After | ForEach-Object { [math]::Round($_[$n].bytes / $cnt) } | Sort-Object -Unique '| {0} | {1:N0} B | {2:N0} B | {3} | {4:N2} µs | {5:N2} µs | {6} |' -f ($n -replace '\|', '\|'), $bb, $ab, (Pct $bb $ab), $bt, $at, (Pct $bt $at) if ($bAll.Count -gt 1 -or $aAll.Count -gt 1) { Write-Host "note: allocation varied across runs for $n" } } Remove-Item $workerPath -ErrorAction SilentlyContinue Run: ./bench-where-object.ps1 -Before /pwsh -After /pwsh Testing I have included a debug script below in the first comment to step through the changes. Pester results (engine/ParameterBinding, engine/Basic, Language, Microsoft.PowerShell.Core/Utility/Management) were identical before and after. PR Checklist PR has a meaningful title Use the present tense and imperative mood when describing your changes Summarized changes Make sure all .h, .cpp, .cs, .ps1 and .psm1 files have the correct copyright header This PR is ready to merge. Breaking changes None User-facing changes Not Applicable Testing - New and feature N/A or can only be tested interactively (behavior is unchanged; covered by existing parameter binding tests)
Open Graph Description: PR Summary Skip mandatory-parameter bookkeeping for parameters that are not in any of the currently valid parameter sets. For every pipeline object, CmdletParameterBinderController.GetMissingMandat...
X Description: PR Summary Skip mandatory-parameter bookkeeping for parameters that are not in any of the currently valid parameter sets. For every pipeline object, CmdletParameterBinderController.GetMissingMandat...
Opengraph URL: https://github.com/PowerShell/PowerShell/pull/28371
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:a104bb1b-282d-3e84-67d3-3eb1408c7689 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | A206:3F5D93:DA1F6:12E488:6AFCDC26 |
| html-safe-nonce | cebbec1664c03e6bfc659da8721ff2d0e6099d4e6bfa017dc00bf488183a45d9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMjA2OjNGNUQ5MzpEQTFGNjoxMkU0ODg6NkFGQ0RDMjYiLCJ2aXNpdG9yX2lkIjoiMjgzMjkzNzE5ODc1ODQ1MjI2MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e8afbb7f992d5e238539e038ba30f052b26a8afd374b3c6702cd4de4e2f6057a |
| hovercard-subject-tag | pull_request:5188205959 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/PowerShell/PowerShell/pull/28371/files |
| twitter:image | https://avatars.githubusercontent.com/u/56769700?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/56769700?s=400&v=4 |
| og:image:alt | PR Summary Skip mandatory-parameter bookkeeping for parameters that are not in any of the currently valid parameter sets. For every pipeline object, CmdletParameterBinderController.GetMissingMandat... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0c05b7eb5b95e29725d01bb1f560a020e3fafd0f033c6b9ed3918fa3161254dd |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 79d9154e55368884c8c93766ba4fee076343d37f |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width