From 98397f939061be2bf35d9cb0e4573eb84fb52aac Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 5 Oct 2023 15:50:45 +0200 Subject: [PATCH] Minimize AllocateTypeAssociatedMemory binary increase --- .../ComputeSharp.D2D1.SourceGenerators.csproj | 1 + .../Interop/Effects/PixelShaderEffect.cs | 5 +-- .../D2D1ResourceTextureManagerImpl.cs | 6 ++-- .../D2D1DrawInfoUpdateContextImpl.cs | 6 ++-- .../D2D1TransformMapperImpl.cs | 6 ++-- .../Helpers/D2D1AssemblyAssociatedMemory.cs | 31 +++++++++++++++++++ .../Translation/D3DCompiler.ID3DInclude.cs | 4 +-- 7 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 src/ComputeSharp.D2D1/Shaders/Interop/Helpers/D2D1AssemblyAssociatedMemory.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index 934ad644..bc2eb6e7 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -82,6 +82,7 @@ + diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs index 38e87929..9ed9f6ac 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs @@ -6,9 +6,6 @@ using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; -#if !NET6_0_OR_GREATER -using RuntimeHelpers = ComputeSharp.NetStandard.RuntimeHelpers; -#endif namespace ComputeSharp.D2D1.Interop.Effects; @@ -47,7 +44,7 @@ internal unsafe partial struct PixelShaderEffect /// The combined vtable for and . private static void** InitVtblForID2D1EffectImplAndID2D1DrawTransform() { - void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(PixelShaderEffect), sizeof(void*) * 14); + void** lpVtbl = (void**)D2D1AssemblyAssociatedMemory.Allocate(sizeof(void*) * 14); // ID2D1EffectImpl #if NET6_0_OR_GREATER diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs index a5fe07c4..c0ac8d04 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs @@ -2,11 +2,9 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; +using ComputeSharp.D2D1.Interop; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; -#if !NET6_0_OR_GREATER -using RuntimeHelpers = ComputeSharp.NetStandard.RuntimeHelpers; -#endif namespace ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; @@ -45,7 +43,7 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl /// The combined vtable for and . private static void** InitVtblForID2D1ResourceTextureManagerAndID2D1ResourceTextureManagerInternal() { - void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(D2D1ResourceTextureManagerImpl), sizeof(void*) * 10); + void** lpVtbl = (void**)D2D1AssemblyAssociatedMemory.Allocate(sizeof(void*) * 10); // ID2D1ResourceTextureManager #if NET6_0_OR_GREATER diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.cs index c49d0fdf..f24d260f 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.cs @@ -2,11 +2,9 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; +using ComputeSharp.D2D1.Interop; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; -#if !NET6_0_OR_GREATER -using RuntimeHelpers = ComputeSharp.NetStandard.RuntimeHelpers; -#endif namespace ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; @@ -45,7 +43,7 @@ internal unsafe partial struct D2D1DrawInfoUpdateContextImpl /// The combined vtable for and . private static void** InitVtblForID2D1DrawInfoUpdateContextAndID2D1DrawInfoUpdateContextInternal() { - void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(D2D1DrawInfoUpdateContextImpl), sizeof(void*) * 10); + void** lpVtbl = (void**)D2D1AssemblyAssociatedMemory.Allocate(sizeof(void*) * 10); // ID2D1ResourceTextureManager #if NET6_0_OR_GREATER diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1TransformMapperImpl.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1TransformMapperImpl.cs index 6a7141ab..a4da3d99 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1TransformMapperImpl.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1TransformMapperImpl.cs @@ -3,11 +3,9 @@ using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; +using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Interop.Effects; using TerraFX.Interop.Windows; -#if !NET6_0_OR_GREATER -using RuntimeHelpers = ComputeSharp.NetStandard.RuntimeHelpers; -#endif namespace ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; @@ -46,7 +44,7 @@ internal unsafe partial struct D2D1TransformMapperImpl /// The combined vtable for and . private static void** InitVtblForID2D1TransformMapperAndID2D1TransformMapperInternal() { - void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(D2D1TransformMapperImpl), sizeof(void*) * 10); + void** lpVtbl = (void**)D2D1AssemblyAssociatedMemory.Allocate(sizeof(void*) * 10); // ID2D1ResourceTextureManager #if NET6_0_OR_GREATER diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/D2D1AssemblyAssociatedMemory.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/D2D1AssemblyAssociatedMemory.cs new file mode 100644 index 00000000..c783bea8 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/D2D1AssemblyAssociatedMemory.cs @@ -0,0 +1,31 @@ +#if NET6_0_OR_GREATER +using System.Runtime.CompilerServices; +#else +using RuntimeHelpers = ComputeSharp.NetStandard.RuntimeHelpers; +#endif + +namespace ComputeSharp.D2D1.Interop; + +/// +/// A helper type to allocate memory that's associated with the current assembly. +/// +internal static unsafe class D2D1AssemblyAssociatedMemory +{ + /// + /// Allocate memory that is associated with the current assembly, and it will be freed if and when it is unloaded. + /// + /// The amount of memory in bytes to allocate. + /// The allocated memory. + public static unsafe void* Allocate(int size) + { + // This is a size optimization specifically for AOT scenarios. The RuntimeHelpers.AllocateTypeAssociatedMemory API + // requires a Type instance being passed to it, and doing typeof(T) ends up rooting a fair amount of extra metadata + // and code for those types. This is especially the case for value types, as the reflection stack assumea that the + // type T might be boxed, and therefore allocates code to handle equality on boxed instances, which also causes + // implemented interfaces (eg. IEquatable) to be rooted, if present. This method works around this issue by + // only invoking this API on an empty type, which minimizes the codegen increase. This is still safe, as types are + // not unloaded one by one. Rather, the entire assembly they belong to is unloaded. So given all uses of this API + // are from within this same assembly, there is no functional difference than associating memory to the various types. + return (void*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(D2D1AssemblyAssociatedMemory), size); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.ID3DInclude.cs b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.ID3DInclude.cs index 9f4065fb..ad7b1c5b 100644 --- a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.ID3DInclude.cs +++ b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.ID3DInclude.cs @@ -1,13 +1,13 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using ComputeSharp.D2D1.Interop; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; #if NET6_0_OR_GREATER using MemoryMarshal2 = System.Runtime.InteropServices.MemoryMarshal; #else using MemoryMarshal2 = ComputeSharp.NetStandard.MemoryMarshal; -using RuntimeHelpers = ComputeSharp.NetStandard.RuntimeHelpers; #endif namespace ComputeSharp.D2D1.Shaders.Translation; @@ -51,7 +51,7 @@ partial class D3DCompiler /// The method table pointer for . private static void** InitVtbl() { - void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(ID3DIncludeForD2DHelpers), sizeof(void*) * 2); + void** lpVtbl = (void**)D2D1AssemblyAssociatedMemory.Allocate(sizeof(void*) * 2); #if NET6_0_OR_GREATER lpVtbl[0] = (delegate* unmanaged)&Open;