-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Make ForEach-Object faster for its commonly used scenarios
#10454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0dbf683
Make ForEach-Object 6x faster for its common usage by rewriting the c…
daxian-dbw efaa319
Set the parent of the rewritten script block
daxian-dbw fb0e9df
Skip the optimization for foreach-object when debugger is enabled
daxian-dbw 3d2cf3f
Add comments about the target syntax for optimization
daxian-dbw 2e4e1b1
Skip optimization when constrained language mode has been used
daxian-dbw c34fdd2
Address Rob and Paul's comment about early return
daxian-dbw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
44 changes: 44 additions & 0 deletions
44
test/powershell/Modules/Microsoft.PowerShell.Core/ForEach-Object.Tests.ps1
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
|
|
||
| Describe "ForEach-Object" -Tags "CI" { | ||
| BeforeAll { | ||
| $testModulePsd1 = Join-Path $TestDrive "ForEachObjectTest.psd1" | ||
| $testModulePsm1 = Join-Path $TestDrive "ForEachObjectTest.psm1" | ||
| Set-Content -Path $testModulePsm1 -Value @' | ||
| function Zoo { "ForEachObjectTest-Zoo" } | ||
| function GetScriptBlock { return { Zoo } } | ||
| '@ | ||
| New-ModuleManifest -Path $testModulePsd1 -RootModule $testModulePsm1 -FunctionsToExport "GetScriptBlock" | ||
| Import-Module $testModulePsd1 | ||
| } | ||
|
|
||
| AfterAll { | ||
| Remove-Module -Name ForEachObjectTest | ||
| } | ||
|
|
||
| It "Foreach-Object should execute script block in caller scope" { | ||
| $null = 1..2 | ForEach-Object { $bar = 100 } | ||
| Get-Variable -Name bar -Scope 0 -ValueOnly | Should -BeExactly 100 | ||
| } | ||
|
|
||
| It "Foreach-Object should execute script block in caller scope regardless of the invocation operator in use" { | ||
| $null = 1..2 | . ForEach-Object { $bar = "bar" } | ||
| $null = 1..2 | & ForEach-Object { $foo = "foo" } | ||
|
|
||
| Get-Variable -Name bar -Scope 0 -ValueOnly | Should -BeExactly "bar" | ||
| Get-Variable -Name foo -Scope 0 -ValueOnly | Should -BeExactly "foo" | ||
| } | ||
|
|
||
| It "Foreach-Object should execute script block in the module scope if specified" { | ||
| { 1 | ForEach-Object { Zoo } } | Should -Throw -ErrorId "CommandNotFoundException" | ||
|
|
||
| $m = Get-Module ForEachObjectTest | ||
| 1 | & $m ForEach-Object { Zoo } | Should -BeExactly "ForEachObjectTest-Zoo" | ||
| } | ||
|
|
||
| It "ForEach-Object should execute script block in the session state that the script block is associated with" { | ||
| $sbToUse = GetScriptBlock | ||
| 1 | ForEach-Object $sbToUse | Should -BeExactly "ForEachObjectTest-Zoo" | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.