From 26fec690f506a430470cd0fb02e2a1ea6d5b6faa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Mon, 31 Jul 2023 16:10:28 +0900 Subject: [PATCH] Debloat serialization guard Use `UnsafeAccessor` instead of a delegate. Shrinks the size of a `PublishAot` app that just `Process.Start`s a new process from 2.2 MB to 1.7 MB. It's still half a MB bigger than what I would expect but now it's `Process`'s fault (the `ToString()` brings some serious amount of garbage and we can't practically trim `ToString`). Fixes #87470. --- .../Serialization/SerializationGuard.cs | 27 ++++--------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/src/libraries/Common/src/System/Runtime/Serialization/SerializationGuard.cs b/src/libraries/Common/src/System/Runtime/Serialization/SerializationGuard.cs index 467ba639e79989..fa87cf9c7bb214 100644 --- a/src/libraries/Common/src/System/Runtime/Serialization/SerializationGuard.cs +++ b/src/libraries/Common/src/System/Runtime/Serialization/SerializationGuard.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Reflection; +using System.Runtime.CompilerServices; namespace System.Runtime.Serialization { @@ -10,34 +11,16 @@ namespace System.Runtime.Serialization /// internal static partial class SerializationGuard { - private delegate void ThrowIfDeserializationInProgressWithSwitchDel(string switchName, ref int cachedValue); - private static readonly ThrowIfDeserializationInProgressWithSwitchDel? s_throwIfDeserializationInProgressWithSwitch = CreateThrowIfDeserializationInProgressWithSwitchDelegate(); - - /// - /// Builds a wrapper delegate for SerializationInfo.ThrowIfDeserializationInProgress(string, ref int), - /// since it is not exposed via contracts. - /// - private static ThrowIfDeserializationInProgressWithSwitchDel? CreateThrowIfDeserializationInProgressWithSwitchDelegate() - { - ThrowIfDeserializationInProgressWithSwitchDel? throwIfDeserializationInProgressDelegate = null; - MethodInfo? throwMethod = typeof(SerializationInfo).GetMethod("ThrowIfDeserializationInProgress", - BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public, null, new Type[] { typeof(string), typeof(int).MakeByRefType() }, Array.Empty()); - - if (throwMethod != null) - { - throwIfDeserializationInProgressDelegate = throwMethod.CreateDelegate(); - } - - return throwIfDeserializationInProgressDelegate; - } - /// /// Provides access to the internal "ThrowIfDeserializationInProgress" method on . /// No-ops if the Serialization Guard feature is disabled or unavailable. /// public static void ThrowIfDeserializationInProgress(string switchSuffix, ref int cachedValue) { - s_throwIfDeserializationInProgressWithSwitch?.Invoke(switchSuffix, ref cachedValue); + ThrowIfDeserializationInProgress(null, switchSuffix, ref cachedValue); + + [UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "ThrowIfDeserializationInProgress")] + static extern void ThrowIfDeserializationInProgress(SerializationInfo? thisPtr, string switchSuffix, ref int cachedValue); } } }