Skip to content

Commit

Permalink
Merge pull request #9711 from h3xds1nz/12ms-speedup-in-startup
Browse files Browse the repository at this point in the history
Improve start-up time by not figuring out NetFX version for AppContext switches
  • Loading branch information
harshit7962 authored Oct 1, 2024
2 parents 812bbba + 9a4f831 commit 0d959af
Showing 1 changed file with 11 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ internal static partial class AppContextDefaultValues
{
public static void PopulateDefaultValues()
{
string platformIdentifier, profile;
int version;

ParseTargetFrameworkName(out platformIdentifier, out profile, out version);
#if NETFX
// Get Target Framework information
ParseTargetFrameworkName(out string platformIdentifier, out string profile, out int version);

// Call into each library to populate their default switches
PopulateDefaultValuesPartial(platformIdentifier, profile, version);
#else
// Call into each library to populate their default switches
// ".NETCoreApp,Version=v3.0"
PopulateDefaultValuesPartial(".NETCoreApp", string.Empty, 30000);
#endif
}

#if NETFX
/// <summary>
/// We have this separate method for getting the parsed elements out of the TargetFrameworkName so we can
/// more easily support this on other platforms.
Expand Down Expand Up @@ -67,29 +71,10 @@ private static void ParseTargetFrameworkName(out string identifier, out string p
/// <summary>
/// This is equivalent to calling <code>AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName</code>
/// </summary>
/// <remarks>
/// <code>AppDomain.CurrentDomain.SetupInformation</code> is not available until .NET Core 3.0, but we
/// have a need to run this code in .NET Core 2.2, we attempt to obtain this information via Reflection.
/// </remarks>
/// <returns>TargetFrameworkMoniker on .NET Framework and .NET Core 3.0+; null on .NET Core 2.2 or older runtimes</returns>
private static string GetTargetFrameworkMoniker()
{
try
{
var pSetupInformation = typeof(AppDomain).GetProperty("SetupInformation");
object appDomainSetup = pSetupInformation?.GetValue(AppDomain.CurrentDomain);
Type tAppDomainSetup = Type.GetType("System.AppDomainSetup");
var pTargetFrameworkName = tAppDomainSetup?.GetProperty("TargetFrameworkName");

return
appDomainSetup != null ?
pTargetFrameworkName?.GetValue(appDomainSetup) as string :
null;
}
catch (Exception)
{
return null;
}
return AppContext.TargetFrameworkName;
}

// This code was a constructor copied from the FrameworkName class, which is located in System.dll.
Expand Down Expand Up @@ -196,7 +181,7 @@ private static bool TryParseFrameworkName(String frameworkName, out String ident

return true;
}

#endif
// This is a partial method. Platforms (such as Desktop) can provide an implementation of it that will read override value
// from whatever mechanism is available on that platform. If no implementation is provided, the compiler is going to remove the calls
// to it from the code
Expand Down

0 comments on commit 0d959af

Please # to comment.