Improve startup time by triggering initialization of additional types on background thread (#18195)

This commit is contained in:
Dongbo Wang
2022-10-04 10:17:09 -07:00
committed by GitHub
parent 19ea6fb4a5
commit 0e4358cf93
@@ -44,22 +44,28 @@ namespace System.Management.Automation.Runspaces
// We shouldn't create too many tasks.
#if !UNIX
// Amsi initialize can be a little slow
// Amsi initialize can be a little slow.
Task.Run(() => AmsiUtils.WinScanContent(content: string.Empty, sourceMetadata: string.Empty, warmUp: true));
#endif
// Initialize the types 'Compiler', 'CachedReflectionInfo', and 'ExpressionCache'.
// Their type initializers do a lot of reflection operations.
// We will access 'Compiler' members when creating the first session state.
Task.Run(() => _ = Compiler.DottedLocalsTupleType);
// One other task for other stuff that's faster, but still a little slow.
Task.Run(() =>
{
// Loading the resources for System.Management.Automation can be expensive, so force that to
// happen early on a background thread.
// Loading the resources for System.Management.Automation can be expensive,
// so force that to happen early on a background thread.
_ = RunspaceInit.OutputEncodingDescription;
// This will init some tables and could load some assemblies.
_ = TypeAccelerators.builtinTypeAccelerators;
// We will access 'LanguagePrimitives' when binding built-in variables for the Runspace.
LanguagePrimitives.GetEnumerator(null);
// This will init some tables and could load some assemblies.
LanguagePrimitives.GetEnumerator(null);
// We will access 'TypeAccelerators' when auto-loading the PSReadLine module, which happens last.
_ = TypeAccelerators.builtinTypeAccelerators;
});
}
}