From 9a14b9bfad900daa9e3019f6978527af1508f490 Mon Sep 17 00:00:00 2001 From: Jackbennett Date: Wed, 15 Feb 2017 12:50:23 +0000 Subject: [PATCH] Faster dotSource implementation for parsing autoload tab-complete This[1] blog post describes a faster method of importing use script into the powershell session by loading the file contents into memory proir to letting powershell parse them for auto-complete values. Reference: 1: https://becomelotr.wordpress.com/2017/02/13/expensive-dot-sourcing/ Testing: Test #1 --- dotsource(ms) | scriptblock(ms) | filename(test data) ------|------|---- 18 | 13 | Approve-Script.ps1 6 | 2 | Clear-PrinterQueue.ps1 14 | 3 | ConvertTo-ImageFormat.ps1 9 | 2 | Copy-MultipleItems.ps1 6 | 2 | Enter-RemoteSession.ps1 6 | 3 | Get-CurrentUser.ps1 7 | 3 | Get-LogonHistory.ps1 6 | 2 | Get-SSID.ps1 6 | 2 | Invoke-DscPullAndApply.ps1 8 | 3 | Move-Drive.ps1 7 | 2 | New-ComputerList.ps1 6 | 2 | New-Directory.ps1 6 | 3 | Set-Proxy.ps1 7 | 2 | Shortcut.ps1 6 | 3 | Start-Delprof.ps1 6 | 3 | Suspend-Computer.ps1 9 | 4 | SystemInformation.ps1 6 | 3 | Test-ModuleSpeed.ps1 7 | 5 | Test-TCPConnection.ps1 6 | 2 | Update-ModuleReadMe.ps1 11 | 3 | Watch-Here.ps1 Test #2 --- dotsource(ms) | scriptblock(ms) | filename(test data) ------|------|----- 40 | 24 | Approve-Script.ps1 8 | 4 | Clear-PrinterQueue.ps1 24 | 12 | ConvertTo-ImageFormat.ps1 14 | 8 | Copy-MultipleItems.ps1 7 | 4 | Enter-RemoteSession.ps1 10 | 4 | Get-CurrentUser.ps1 30 | 6 | Get-LogonHistory.ps1 6 | 4 | Get-SSID.ps1 7 | 4 | Invoke-DscPullAndApply.ps1 11 | 8 | Move-Drive.ps1 11 | 5 | New-ComputerList.ps1 11 | 4 | New-Directory.ps1 6 | 5 | Set-Proxy.ps1 11 | 5 | Shortcut.ps1 10 | 12 | Start-Delprof.ps1 12 | 5 | Suspend-Computer.ps1 69 | 5 | SystemInformation.ps1 11 | 4 | Test-ModuleSpeed.ps1 6 | 7 | Test-TCPConnection.ps1 8 | 4 | Update-ModuleReadMe.ps1 10 | 9 | Watch-Here.ps1 --- vendor/profile.ps1 | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/vendor/profile.ps1 b/vendor/profile.ps1 index 7878ff2d2..97ea6272f 100644 --- a/vendor/profile.ps1 +++ b/vendor/profile.ps1 @@ -92,12 +92,15 @@ if (-not (test-path "$ENV:CMDER_ROOT\config\profile.d")) { mkdir "$ENV:CMDER_ROOT\config\profile.d" } -pushd $ENV:CMDER_ROOT\config\profile.d -foreach ($x in ls *.ps1) { - # write-host write-host Sourcing $x - . $x +foreach ($file in (Get-ChildItem "$($env:CMDER_ROOT)\config\profile.d" -Filter '*.ps1') ) { + # Grab the contents of files at once and parse that as a script block. + # faster script loading, see: https://becomelotr.wordpress.com/2017/02/13/expensive-dot-sourcing/ + . ( + [scriptblock]::Create( + [io.file]::ReadAllText($file.fullname) + ) + ) } -popd # # Prompt Section