Skip to content

Read the mock parameter filter marker directly instead of Get-Variable#2916

Open
nohwnd wants to merge 1 commit into
mainfrom
nohwnd-perf-mock-marker
Open

Read the mock parameter filter marker directly instead of Get-Variable#2916
nohwnd wants to merge 1 commit into
mainfrom
nohwnd-perf-mock-marker

Conversation

@nohwnd

@nohwnd nohwnd commented Jul 19, 2026

Copy link
Copy Markdown
Member

Test-ParameterFilter called Get-Variable -Scope Script -ErrorAction Ignore to save the previous value of ______isInMockParameterFilter before every parameter filter evaluation, and Set-AssertionPassResult did the same lookup on every passing Should-* assertion. Get-Variable with -ErrorAction Ignore is expensive for something that runs this often, and the only reason for it was that the variable might not exist yet.

The variable is now initialized once at module scope to $false, so both places do a direct $script: read, which also stays safe under Set-StrictMode. The save/restore in Test-ParameterFilter becomes a plain assignment, without the conditional Remove-Variable branch.

Two small ride-alongs in the same file: Get-ContextToDefine inlines Get-ConflictingParameterNames (its whole body is the same $script: variable read the function already does directly a few lines below), and an @() wrapper is dropped around an alias collection that already has .Count.

Verification: full suite green on macOS (inline and dot-sourced build), Pester.Mock.RSpec.ts.ps1 and the Mock.Tests.ps1 suite pass, including the tests that mix Should inside -ParameterFilter where the marker controls throw-vs-collect behavior.

Related perf PRs: #2914, #2915, #2917, #2918. Measured together they cut ~21% from four representative workloads (inline build, macOS).

🤖

Test-ParameterFilter called Get-Variable -ErrorAction Ignore to save the
previous value of ______isInMockParameterFilter before every filter
evaluation, and Set-AssertionPassResult did the same on every passing
Should-* assertion. The variable is now initialized once at module scope,
so a direct $script: read works, is cheap, and is safe under strict mode,
and the save/restore is a plain assignment without the conditional
Remove-Variable.

Also inlined Get-ConflictingParameterNames in Get-ContextToDefine (its body
is the same variable read the function already does directly further down)
and dropped an @() around an alias collection that already has .Count.

🤖
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant