feO*_*O2x 2 c# memory performance enums garbage-collection
我想要一种快速通用的方法来检查标记为 的枚举值是否FlagsAttribute有效。为此,我创建了一个名为 的类,EnumInfo<T>该类通过按位或运算枚举的所有不同值来计算其静态构造函数中的标志模式。然后,该IsValidFlagsValue方法通过与标志模式执行按位 AND 比较来简单地检查提供的值。
考虑以下 C# 代码
// This code requires C# 7.3
public static class EnumInfo<T> where T : Enum, IConvertible
{
// This field is calculated in the static constructor
public static readonly ulong FlagsPattern;
public static bool IsValidFlagsValue(this T enumValue)
{
// The actual problem is here: ToUInt64 allocates (because of internal boxing?)
var convertedUInt64Value = enumValue.ToUInt64(null);
return (FlagsPattern & convertedUInt64Value) == convertedUInt64Value;
}
}
Run Code Online (Sandbox Code Playgroud)
我的实际问题如下:为了重用此代码,我将枚举值转换为类型ulong。但是,内部分配 24 字节,如以下基准测试(使用BenchmarkDotNetenumValue.ToUInt64(null)执行)所示:
public class ToUInt64Benchmark
{
public IConvertible FlagsValue =
BindingFlags.Public | BindingFlags.Instance;
[Benchmark]
public ulong EnumToUInt64() => FlagsValue.ToUInt64(null);
}
Run Code Online (Sandbox Code Playgroud)
目前有什么办法可以避免这种分配吗?我尝试使用不安全的代码,但无法获取通用值的地址(即&enumValue不起作用)。是否还有另一种我没有想到的方法可以做到这一点?
提前感谢您的帮助。
正如pinkfloydx33在这个问题的评论中指出的那样,Unsafe在System.Runtime.CompilerServices.Unsafe NuGet包中有一个名为的类。有了它,您可以以不安全的方式强制转换为 ulong,但不会分配。如果我们从我的问题中修改基准类,如下所示:
public class ToUInt64Benchmark
{
public BindingFlags FlagsValue = BindingFlags.Public | BindingFlags.Instance;
public IConvertible FlagsValueAsConvertible;
public ToUInt64Benchmark() => FlagsValueAsConvertible = FlagsValue;
[Benchmark(Baseline = true)]
public ulong EnumToUInt64() => FlagsValueAsConvertible.ToUInt64(null);
[Benchmark]
public ulong UnsafeEnumToUInt64() => ConvertUnsafe(FlagsValue);
private static unsafe ulong ConvertUnsafe<T>(T enumValue) where T : struct, Enum
{
var pointer = (ulong*)Unsafe.AsPointer(ref enumValue);
return *pointer;
}
}
Run Code Online (Sandbox Code Playgroud)
...这会导致以下结果:
它速度更快(只有 Surface Pro 4 上 .NET Core 2.2 执行时间的 10% ToUInt64),最重要的是,它不分配。
请务必将该AllowUnsafeBlocks标签添加到您的 csproj 文件中,以允许编译不安全的代码。您无法在部分受信任的平台(例如 Silverlight)上运行此代码。这是我的 csproj 文件的结构。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp2.2;net472</TargetFrameworks>
<DebugType>portable</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.11.3" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
正如Pinkfloydx33在评论中正确指出的那样(再次),可以使用以下方法简化代码Unsafe.As:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp2.2;net472</TargetFrameworks>
<DebugType>portable</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.11.3" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
这对性能没有影响,但不需要AllowUnsafeBlockscsproj 文件中的标记。
| 归档时间: |
|
| 查看次数: |
360 次 |
| 最近记录: |