Minimize AllocateTypeAssociatedMemory binary increase

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-08 17:59:35 +02:00
1 親 392cf5c0a5
コミット 98397f9390
7ファイル変更
+41 -18

検索条件に一致するファイルはありません

+1
ファイルの表示
@@ -82,6 +82,7 @@
<Compile Include="..\ComputeSharp.D2D1\Interfaces\Descriptors\ID2D1PixelShaderDescriptor{T}.cs" Link="ComputeSharp.D2D1\Interfaces\Descriptors\ID2D1PixelShaderDescriptor{T}.cs" />
<Compile Include="..\ComputeSharp.D2D1\Intrinsics\D2D.cs" Link="ComputeSharp.D2D1\Intrinsics\D2D.cs" />
<Compile Include="..\ComputeSharp.D2D1\Shaders\Exceptions\FxcCompilationException.cs" Link="ComputeSharp.D2D1\Shaders\Exceptions\FxcCompilationException.cs" />
<Compile Include="..\ComputeSharp.D2D1\Shaders\Interop\Helpers\D2D1AssemblyAssociatedMemory.cs" Link="ComputeSharp.D2D1\Shaders\Interop\Helpers\D2D1AssemblyAssociatedMemory.cs" />
<Compile Include="..\ComputeSharp.D2D1\Shaders\Interop\D2D1InputDescription.cs" Link="ComputeSharp.D2D1\Shaders\Interop\D2D1InputDescription.cs" />
<Compile Include="..\ComputeSharp.D2D1\Shaders\Interop\D2D1PixelShaderInputType.cs" Link="ComputeSharp.D2D1\Shaders\Interop\D2D1PixelShaderInputType.cs" />
<Compile Include="..\ComputeSharp.D2D1\Shaders\Interop\D2D1ResourceTextureDescription.cs" Link="ComputeSharp.D2D1\Shaders\Interop\D2D1ResourceTextureDescription.cs" />
+1 -4
ファイルの表示
@@ -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
/// <returns>The combined vtable for <see cref="ID2D1EffectImpl"/> and <see cref="ID2D1DrawTransform"/>.</returns>
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
@@ -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
/// <returns>The combined vtable for <see cref="ID2D1ResourceTextureManager"/> and <see cref="ID2D1ResourceTextureManagerInternal"/>.</returns>
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
@@ -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
/// <returns>The combined vtable for <see cref="ID2D1DrawInfoUpdateContext"/> and <see cref="ID2D1DrawInfoUpdateContextInternal"/>.</returns>
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
@@ -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
/// <returns>The combined vtable for <see cref="ID2D1TransformMapper"/> and <see cref="ID2D1TransformMapperInternal"/>.</returns>
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
+31
ファイルの表示
@@ -0,0 +1,31 @@
#if NET6_0_OR_GREATER
using System.Runtime.CompilerServices;
#else
using RuntimeHelpers = ComputeSharp.NetStandard.RuntimeHelpers;
#endif
namespace ComputeSharp.D2D1.Interop;
/// <summary>
/// A helper type to allocate memory that's associated with the current assembly.
/// </summary>
internal static unsafe class D2D1AssemblyAssociatedMemory
{
/// <summary>
/// Allocate memory that is associated with the current assembly, and it will be freed if and when it is unloaded.
/// </summary>
/// <param name="size">The amount of memory in bytes to allocate.</param>
/// <returns>The allocated memory.</returns>
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<T>) 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);
}
}
+2 -2
ファイルの表示
@@ -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
/// <returns>The method table pointer for <see cref="ID3DIncludeForD2DHelpers"/>.</returns>
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<ID3DIncludeForD2DHelpers*, D3D_INCLUDE_TYPE, sbyte*, void*, void**, uint*, int>)&Open;