Skip to content

pwsh is 2 times slower than powershell to load #6443

Closed

Description

@musm

Powershell 6 is slow to start up compared to powershell

function time($block) {
    $sw = [Diagnostics.Stopwatch]::StartNew()
    &$block
    $sw.Stop()
    $sw.Elapsed
}

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      17115  1

vs

Major  Minor  Patch  PreReleas BuildLabel
                     eLabel
-----  -----  -----  --------- ----------
6      0      2


mus@mus-laptop ~
> time {powershell -NoProfile -c exit}


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 233
Ticks             : 2337327
TotalDays         : 2.70523958333333E-06
TotalHours        : 6.492575E-05
TotalMinutes      : 0.003895545
TotalSeconds      : 0.2337327
TotalMilliseconds : 233.7327



mus@mus-laptop ~
> time {pwsh -NoProfile -c exit}


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 540
Ticks             : 5400421
TotalDays         : 6.25048726851852E-06
TotalHours        : 0.000150011694444444
TotalMinutes      : 0.00900070166666667
TotalSeconds      : 0.5400421
TotalMilliseconds : 540.0421





> time {cmd /c exit}


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : **42**
Ticks             : 426119
TotalDays         : 4.93193287037037E-07
TotalHours        : 1.18366388888889E-05
TotalMinutes      : 0.000710198333333333
TotalSeconds      : 0.0426119
TotalMilliseconds : 42.6119



Winver: 17115.1

Activity

  1. changed the title [-]pwsh is almost 3 times slower than powershell to load[/-] [+]pwsh is 2 times slower than powershell to load[/+] on Mar 20, 2018
  2. added
    WG-Engine-Performance
    on Mar 21, 2018
  3. SteveL-MSFT commented on Mar 21, 2018

    @SteveL-MSFT
    Member

    Looks like assemblies aren't being crossgen'd so startup time is affected by JIT

    PS C:\Program Files\PowerShell\6.0.1> [System.Reflection.AssemblyName]::GetAssemblyName("${pwd}\System.Management.Automation.dll") | fl *
    
    
    Name                  : System.Management.Automation
    Version               : 6.0.1.0
    CultureInfo           :
    CultureName           :
    CodeBase              :
    EscapedCodeBase       :
    ProcessorArchitecture : MSIL
    ContentType           : Default
    Flags                 : PublicKey
    HashAlgorithm         : SHA1
    VersionCompatibility  : SameMachine
    KeyPair               :
    FullName              : System.Management.Automation, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
  4. SteveL-MSFT commented on Mar 21, 2018

    @SteveL-MSFT
    Member

    Looks like assemblies are being crossgen'd. However, getting many messages from crossgen.exe that certain things aren't supported, which means those methods stay as MSIL (which probably explains why the assembly says it's MSIL). This also means that the MSIL needs to be JIT on startup. I think this is what's impacting the startup performance. We'll need to continue this investigation to see if anything we can do here.

  5. musm commented on Mar 21, 2018

    @musm
    Author

    Honestly even powershell 5 is very slow (6 times slower) than CMD. It would be good to have powershell 6 within 2~3 times the speed of cmd.

    For example on my computer python is only 2 times slower than CMD to start...

  6. bergmeister commented on Mar 24, 2018

    @bergmeister
    Contributor

    It would be great to have better startup time since custom profiles add on top of that. But honestly, PowerShell easily makes up those seconds when it comes to getting stuff done (not only in terms of time to run but also the time to write the code/command) 😄

  7. musm commented on Mar 25, 2018

    @musm
    Author

    It would be great to have better startup time since custom profiles add on top of that. But honestly, PowerShell easily makes up those seconds when it comes to getting stuff done (not only in terms of time to run but also the time to write the code/command) 😄

    Honestly, this is not a very good argument. Same thing could be said about windows start up times or python start up times. First time to boot/startup is pretty important.

  8. powercode commented on Mar 25, 2018

    @powercode
    Collaborator

    On the other hand, cmd.exe is a native exe that does next to nothing on startup. pwsh.exe is a managed exe that sets up a complex execution environment.

    We should of course strive to improve the startup performance, but comparing to completely different environments with very diffrent capabilities is not really relevant.

  9. musm commented on Mar 25, 2018

    @musm
    Author

    On the other hand, cmd.exe is a native exe that does next to nothing on startup. pwsh.exe is a managed exe that sets up a complex execution environment.

    We should of course strive to improve the startup performance, but comparing to completely different environments with very diffrent capabilities is not really relevant.

    Agreed, which is why I included Powershell 6 and Python 3 performance relative to CMD. It seems that powershell should be able to be within 2-3 times the startup speed of CMD, putting it in line with other interactive languages.

  10. powercode commented on Mar 26, 2018

    @powercode
    Collaborator

    Agreed, which is why I included Powershell 6 and Python 3 performance relative to CMD. It seems that powershell should be able to be within 2-3 times the startup speed of CMD, putting it in line with other interactive languages.

    Being 'interactive' is just one trait of many. The environments are very different.
    PowerShell has an adaptable type system that abstracts many different underlying type systems. It has an extensible formatting system that renders objects to text. It has an object based pipeline. It has an adaptable prompt, tab completion etc.

    All of these takes time to initialize, especially the extensible types and formatting systems.

    As Steve said, there is an issue with the assemblies not being crossgen-ed as they should, so we spend quite a lot of extra time jitting IL. That must be fixed.

    But even with that fixed, there are differences in scope and the dependencies on the CLR/DLR that will make powershell slower to start than cmd and python.

  11. markekraus commented on Mar 26, 2018

    @markekraus
    Contributor

    I would venture to guess that if python were to have a more expressive REPL and a deep framework available and ready to use on the first line, its load times would be on-par with pwsh. I have seen plenty of python scripts with long init times due to dependency loading for more advanced uses. Python starts fast by default because it loads with less available by default. Comparing the start times between PowerShell and other shells and scripting languages is a bit hard to do unless they are all closer in default feature parity. none of the languages I have experience with come with as mush on-by-default as PowerShell does (for better or worse).

  12. musm commented on Mar 30, 2018

    @musm
    Author

    One idea is to do the jitting the first time the function is called (which will make the first time to actually call the function slow, but subsequent calls fast). In addition to this, a set of frequently utilized functions can be precompiled/jitted functions on startup.

  13. 32 remaining items

  14. Bouke commented on Jul 9, 2020

    @Bouke

    The testing here is all without loading a profile (-NoProfile) and looking at startup times of a shell of almost 1 second. I would consider even 0.1 second as being a tad on the slow side.

    Version Startup Time
    5 0.3s
    6.2.3 4.5s
    7.0.2 0.6s

    Mind you that with plugins enabled startup will suffer even more. I have two plugins enabled: PoshGit and Z-AutoJump. My startup time with these two plugins is:

    Version Startup Time
    5 2.0s
    6.2.3 4.8s
    7.0.2 2.2s

    Compare that with startup times on macOS (iTerm):

    Shell Startup Time
    zsh 5.7.1 (no plugins) 0.00s
    zsh 5.7.1 (with lots of plugins) 0.14s
    pwsh 7.0.0 (no plugins) 0.43s
  15. musm commented on Jul 9, 2020

    @musm
    Author

    Bouke Haarsma (@Bouke) totally agree. Note, posh-git is notoriously bad for adding at least 500ms to the start time.

    And yes even 1 second is a ridiculous start time for an interactive shell. Start times need to get under 100ms for the best UX.

  16. isti115 commented on Jul 16, 2020

    @isti115

    As someone, who has pwsh as his default shell under linux, this greatly affects me, so I would much appreciate any possible improvement! Not only is launching a terminal window noticeably slower, but I just cannot have quick powershell scripts run on my hotkeys. (Which I have quite a few of that I rely on, since I'm using sway as a window manger.)

    I need to go back to bash or perl for these purposes, which are much more infuriating to write stuff in, than powershell, which is in my opinion a much better system overall, that's why I'm sticking to it, even though there are quite a few small inconveniences. (Such as for example the '<' file redirection operator missing, which is expected by some programs, such as vim, but luckily it can be fixed by telling it to use pipes instead.)

    Anyway, some updates that make pwsh -c viable for more or less instantaneous small things would be very welcome!
    Thanks for all the work of the development team, my life is already much better by having a shell with proper syntax and programmability (e.g. the | % { } foreach and such things are awesome! That same thing would be so much more complicated in bash...) instead of the "everything's just text, use cut, sed, awk, grep, ..." mess that all the POSIX compatible shells are!

  17. iSazonov commented on Jul 16, 2020

    @iSazonov
    Collaborator

    István Donkó (@isti115) Fundamental problem is that managed runtime initialization is significantly slower than native code. .Net team made many improvements but managed will never be as fast as native at startup.
    In the repo performance improvements was also made. I do not think we can make more in the milestone.

    Cheat: you can create and run simple C# infinite service with one function - load all PowerShell dlls.
    (As result all dlls will be already in memory before you run PowerShell)
    (For the service you can use PowerShell too - run and Start-Sleep -Seconds 65535.)

  18. musm commented on Jul 16, 2020

    @musm
    Author

    Can parts of the initialization be done after the REPL is started?

    1. Initialize necessary code for the REPL
    2. Initialize everything else afterwards.
  19. iSazonov commented on Jul 16, 2020

    @iSazonov
    Collaborator

    I think REPL is more for C# scripts.
    For PowerShell there is a hosting idea from Jason but it is not easy to implement I guess.

  20. isti115 commented on Jul 21, 2020

    @isti115

    Hmm, reading all the suggestions I came up with the idea of a simple PowerShell script running continuously in the background reading a FIFO as it's input and executing whatever commands get piped in there. I know that this is a very messy solution, because variables and contexts could get mixed up, but if I am careful enough when writing the commands and ensure that they have no side effects (I mean, in terms of the execution context, otherwise it would make no sense to run the commands at all. 😄) this could work.

  21. iSazonov commented on Dec 3, 2020

    @iSazonov
    Collaborator

    We started new wave of startup scenario optimizations in #14268. Join us for contributing, reviewing and testing!
    Also we expect many improvements for .Net 6.0 from .Net team - we will benefit from this automatically.

  22. joeyaiello commented on Dec 7, 2020

    @joeyaiello
    Contributor

    Closing in favor of the more actionable issue that Ilya (@iSazonov) wrote (thanks!), #14268

    This is something we should always strive to improve, but I think this thread has gone somewhat stale in terms of actionable technical info.

  23. flawrist commented on Jul 4, 2021

    @flawrist

    Dear fellows, I am tired that PS 5.1 currently installed on my Windows 7 x64 (please, don’t even mention W10) is unable to properly execute BOM-less *.ps1 with UTF8 strings (e.g. $singer = "Björk"; echo $singer outputs as BjГ¶rk). That's why I downloaded both (portable) PS 6.2.7 and 7.2.0p7 and run as follows pwsh.exe .\test.ps1. Indeed, they have no problem outputting UTF8, but the execution time is 2x slower: 320ms (5) vs 694ms (7) vs 760ms (6). Delay is noticeable and annoying.

    I am not a developer to grasp all the aforementioned mumbo jumbo on why the progress turned into regress, but I remember there was a trick to make early versions of PS run faster by ngen install. Is there something similar here, or we're doomed?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    WG-Engine-Performance

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions