提交 5e8c4977 编写于 作者: C CyrusNajmabadi

Add fast path for ToString for this enum.

上级 3ffbbd83
......@@ -52,7 +52,7 @@ public Checksum CreateChecksum(object value, CancellationToken cancellationToken
{
var kind = value.GetWellKnownSynchronizationKind();
using (Logger.LogBlock(FunctionId.Serializer_CreateChecksum, kind.ToString(), cancellationToken))
using (Logger.LogBlock(FunctionId.Serializer_CreateChecksum, kind.ToStringFast(), cancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Linq;
using System.Reflection;
namespace Microsoft.CodeAnalysis.Serialization
{
// TODO: Kind might not actually needed. see whether we can get rid of this
......@@ -47,4 +50,35 @@ internal enum WellKnownSynchronizationKind
ProjectStateChecksums,
DocumentStateChecksums,
}
internal static class WellKnownSynchronizationKindExtensions
{
private static readonly string[] s_strings;
static WellKnownSynchronizationKindExtensions()
{
var fields = typeof(WellKnownSynchronizationKind).GetTypeInfo().DeclaredFields.Where(f => f.IsStatic);
var maxValue = 0;
foreach (var field in fields)
{
var value = (int)field.GetValue(null);
if (value > maxValue)
{
maxValue = value;
}
}
s_strings = new string[maxValue + 1];
foreach (var field in fields)
{
var value = (int)field.GetValue(null);
s_strings[value] = field.Name;
}
}
public static string ToStringFast(this WellKnownSynchronizationKind kind)
=> s_strings[(int)kind];
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册