有没有一种方法可以将通用枚举值转换为UInt64值而无需分配?

时间:2018-12-26 07:45:35

标签: c# performance memory enums garbage-collection

我希望有一种快速的通用方法来检查用FlagsAttribute标记的枚举值是否有效。为此,我创建了一个名为EnumInfo<T>的类,该类通过对枚举的所有不同值进行按位或运算来计算其静态构造函数中的标志模式。然后,IsValidFlagsValue方法仅通过执行与标志模式的按位与比较来检查提供的值。

考虑以下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;
    }
}

我的实际问题是:要重用此代码,我将枚举值转换为类型ulong。但是,enumValue.ToUInt64(null)在内部分配24字节,正如在下面的基准测试中所看到的(执行BenchmarkDotNet):

public class ToUInt64Benchmark
{
    public IConvertible FlagsValue = 
        BindingFlags.Public | BindingFlags.Instance;

    [Benchmark]
    public ulong EnumToUInt64() => FlagsValue.ToUInt64(null);
}

BenchmarkDotNet results

当前是否有任何方法可以避免这种分配?我尝试使用不安全的代码,但是无法获取通用值的地址(即&enumValue无法正常工作)。可能还有另一种我从未想到过的方法吗?

谢谢您的帮助。

2 个答案:

答案 0 :(得分:3)

正如pinkfloydx33在对这些问题的评论中指出的那样,在System.Runtime.CompilerServices.Unsafe NuGet包中有一个名为Unsafe的类。有了它,您就可以不分配的不安全方式投向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;
    }
}

...这将导致以下结果:

Benchmark Results with unsafe code

它的速度更快(在我的Surface Pro 4上.NET Core 2.2中ToUInt64的执行时间仅为10%),而且最重要的是,它没有分配。

请确保将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>

更新2018-12-26 17:00 UTC

pinkfloydx33在注释中正确指出(再次),可以使用Unsafe.As来简化代码:

public class ToUInt64Benchmark
{
    // Other members omitted for brevity's sake
    [Benchmark]
    public ulong UnsafeEnumToUInt64() => ConvertUnsafe(FlagsValue);

    private static ulong ConvertUnsafe<T>(T enumValue) where T : struct, Enum => 
        Unsafe.As<T, ulong>(ref enumValue);
}

这对性能没有影响,但是不需要csproj文件中的AllowUnsafeBlocks标签。

答案 1 :(得分:0)

您不能避免同时装箱使用界面。装箱是因为您在enumValue上调用了接口方法。

如果您不顾一切地避免多余的分配,请忽略接口。拥有完美的OO设计并不是高性能的亲密朋友。