Skip to content

Commit

Permalink
Remove dependency on AppDomain from a hello world app (#95710)
Browse files Browse the repository at this point in the history
* Remove dependency on AppDomain from a hello world app

Saves 4% in size on a hello world.

A hello world app will bring the `AppDomain` type with it. `AppDomain`'s `ToString` is quite expensive for what it does.

* AppDomain trimming

---------

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
  • Loading branch information
MichalStrehovsky and jkotas authored Dec 8, 2023
1 parent 611934f commit b995d33
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,14 @@ internal static void OnFirstChanceException(object e)
}
#endif

internal static event EventHandler? ProcessExit;

internal static void OnProcessExit()
{
AssemblyLoadContext.OnProcessExit();
if (EventSource.IsSupported)
{
EventListener.DisposeOnShutdown();
}

ProcessExit?.Invoke(AppDomain.CurrentDomain, EventArgs.Empty);
AppDomain.OnProcessExit();
}

/// <summary>
Expand Down
25 changes: 20 additions & 5 deletions src/libraries/System.Private.CoreLib/src/System/AppDomain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,28 @@ namespace System
{
public sealed partial class AppDomain : MarshalByRefObject
{
private static readonly AppDomain s_domain = new AppDomain();
private static AppDomain? s_domain;

private IPrincipal? _defaultPrincipal;
private PrincipalPolicy _principalPolicy = PrincipalPolicy.NoPrincipal;
private Func<IPrincipal>? s_getWindowsPrincipal;
private Func<IPrincipal>? s_getUnauthenticatedPrincipal;

private AppDomain() { }

public static AppDomain CurrentDomain => s_domain;
public static AppDomain CurrentDomain
{
get
{
// Create AppDomain instance only once external code asks for it. AppDomain brings lots of unnecessary
// dependencies into minimal trimmed app via ToString method.
if (s_domain == null)
{
Interlocked.CompareExchange(ref s_domain, new AppDomain(), null);
}
return s_domain;
}
}

public string BaseDirectory => AppContext.BaseDirectory;

Expand Down Expand Up @@ -72,10 +85,12 @@ public event EventHandler<FirstChanceExceptionEventArgs>? FirstChanceException
remove { AppContext.FirstChanceException -= value; }
}

public event EventHandler? ProcessExit
public event EventHandler? ProcessExit;

internal static void OnProcessExit()
{
add { AppContext.ProcessExit += value; }
remove { AppContext.ProcessExit -= value; }
AppDomain? domain = s_domain;
domain?.ProcessExit?.Invoke(domain, EventArgs.Empty);
}

public string ApplyPolicy(string assemblyName)
Expand Down

0 comments on commit b995d33

Please sign in to comment.