How to improve Import-Module in $PROFILE ?
kid1412621 opened this issue · 4 comments
run Measure-Script $PROFILE
, got this:
1 33 00:00.0024737 Import-Module Posh-Git
1 34 00:00.0026190 Import-Module DockerCompletion
1 35 00:00.0020116 Import-Module MavenAutoCompletion
1 36 00:00.0022025 Import-Module npm-completion
this is winget comparison:
0 38 00:00.0000000 # Winget
1 39 00:00.0006264 Register-ArgumentCompleter -Native -CommandName winget -ScriptBlock {
0 40 00:00.0000000 param($wordToComplete, $commandAst, $cursorPosition)
0 41 00:00.0000000 [Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding =
[System.Text.Utf8Encoding]::new()
0 42 00:00.0000000 $Local:word = $wordToComplete.Replace('"', '""')
0 43 00:00.0000000 $Local:ast = $commandAst.ToString().Replace('"', '""')
0 44 00:00.0000000 winget complete --word="$Local:word" --commandline "$Local:ast" --position
$cursorPosition | ForEach-Object {
0 45 00:00.0000000 [System.Management.Automation.CompletionResult]::new($_, $_,
'ParameterValue', $_)
0 46 00:00.0000000 }
0 47 00:00.0000000 }
there's a thread talking about this.
@kid1412621 Would you tell me what the Measure-Script
is? It does not seem to be a standard PowerShell cmdlet.
The above example of winget is quite small, while DockerCompletion and others are inherently big and take more time.
One of my idea is to add the following Enable-Completion
function to your $PROFILE
:
function Enable-Completion {
param (
[ValidateSet('docker', 'git')]
[string[]]
$Name = @('docker', 'git')
)
switch ($Name) {
docker { Import-Module DockerCompletion }
git { Import-Module Posh-Git }
}
}
Completion modules are not imported in the $PROFILE
. When you want to import DockerCompletion, invoke Enable-Completion docker
. When you want to import all completion modules, invoke Enable-Completion
without parameters.
@kid1412621 Would you tell me what the
Measure-Script
is? It does not seem to be a standard PowerShell cmdlet.The above example of winget is quite small, while DockerCompletion and others are inherently big and take more time.
One of my idea is to add the following
Enable-Completion
function to your$PROFILE
:function Enable-Completion { param ( [ValidateSet('docker', 'git')] [string[]] $Name = @('docker', 'git') ) switch ($Name) { docker { Import-Module DockerCompletion } git { Import-Module Posh-Git } } }Completion modules are not imported in the
$PROFILE
. When you want to import DockerCompletion, invokeEnable-Completion docker
. When you want to import all completion modules, invokeEnable-Completion
without parameters.
right, it's a third-party module.
Thanks for your offering solution, but I think it's not convenient since I have to enable it every time. The reason I want improve this is the shell boot up time is too long, ~2s. Mainly the oh-my-posh took a long time since powerShell issue, so I expect to reduce the overhead from each other imports.
right, it's a third-party module.
The module only counts the lines of code like this:
PS> Measure-Script $PROFILE
LinesOfCode
-----------
120
so I expect to reduce the overhead from each other imports.
I am not sure of the details of "the overhead" and the general solution of it.
This issue will be closed, but contributions welcome. Please tell me the solution if you find it,
right, it's a third-party module.
The module only counts the lines of code like this:
PS> Measure-Script $PROFILE LinesOfCode ----------- 120so I expect to reduce the overhead from each other imports.
I am not sure of the details of "the overhead" and the general solution of it.
This issue will be closed, but contributions welcome. Please tell me the solution if you find it,
Thx. Btw, sorry for the incorrect url of the third party module. This is the correct one: https://devblogs.microsoft.com/powershell/optimizing-your-profile/#measure-script