Skip to content

Commit 0e4358c

Browse files
authored
Improve startup time by triggering initialization of additional types on background thread (PowerShell#18195)
1 parent 19ea6fb commit 0e4358c

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/System.Management.Automation/engine/InitialSessionState.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,28 @@ internal static void Init()
4444

4545
// We shouldn't create too many tasks.
4646
#if !UNIX
47-
// Amsi initialize can be a little slow
47+
// Amsi initialize can be a little slow.
4848
Task.Run(() => AmsiUtils.WinScanContent(content: string.Empty, sourceMetadata: string.Empty, warmUp: true));
4949
#endif
50+
// Initialize the types 'Compiler', 'CachedReflectionInfo', and 'ExpressionCache'.
51+
// Their type initializers do a lot of reflection operations.
52+
// We will access 'Compiler' members when creating the first session state.
53+
Task.Run(() => _ = Compiler.DottedLocalsTupleType);
5054

5155
// One other task for other stuff that's faster, but still a little slow.
5256
Task.Run(() =>
5357
{
54-
// Loading the resources for System.Management.Automation can be expensive, so force that to
55-
// happen early on a background thread.
58+
// Loading the resources for System.Management.Automation can be expensive,
59+
// so force that to happen early on a background thread.
5660
_ = RunspaceInit.OutputEncodingDescription;
5761

5862
// This will init some tables and could load some assemblies.
59-
_ = TypeAccelerators.builtinTypeAccelerators;
63+
// We will access 'LanguagePrimitives' when binding built-in variables for the Runspace.
64+
LanguagePrimitives.GetEnumerator(null);
6065

6166
// This will init some tables and could load some assemblies.
62-
LanguagePrimitives.GetEnumerator(null);
67+
// We will access 'TypeAccelerators' when auto-loading the PSReadLine module, which happens last.
68+
_ = TypeAccelerators.builtinTypeAccelerators;
6369
});
6470
}
6571
}

0 commit comments

Comments
 (0)