Use ConcurrentDictionary in JsonFormatter

pull/14272/head
James Newton-King 1 year ago
parent e912bc2e3a
commit 00f0f52420
No known key found for this signature in database
GPG Key ID: A66B2F456BF5526
  1. 29
      csharp/src/Google.Protobuf/JsonFormatter.cs

@ -9,15 +9,16 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
using Google.Protobuf.Reflection;
using Google.Protobuf.WellKnownTypes;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Google.Protobuf.Reflection;
using Google.Protobuf.WellKnownTypes;
namespace Google.Protobuf
{
@ -962,25 +963,17 @@ namespace Google.Protobuf
// The need for this is unfortunate, as is its unbounded size, but realistically it shouldn't cause issues.
private static class OriginalEnumValueHelper
{
// TODO: In the future we might want to use ConcurrentDictionary, at the point where all
// the platforms we target have it.
private static readonly Dictionary<System.Type, Dictionary<object, string>> dictionaries
= new Dictionary<System.Type, Dictionary<object, string>>();
private static readonly ConcurrentDictionary<System.Type, Dictionary<object, string>> dictionaries
= new ConcurrentDictionary<System.Type, Dictionary<object, string>>();
[UnconditionalSuppressMessage("Trimming", "IL2072",
Justification = "The field for the value must still be present. It will be returned by reflection, will be in this collection, and its name can be resolved.")]
internal static string GetOriginalName(object value)
{
var enumType = value.GetType();
Dictionary<object, string> nameMapping;
lock (dictionaries)
Dictionary<object, string> nameMapping = dictionaries.GetOrAdd(value.GetType(), static t =>
{
if (!dictionaries.TryGetValue(enumType, out nameMapping))
{
nameMapping = GetNameMapping(enumType);
dictionaries[enumType] = nameMapping;
}
}
return GetNameMapping(t);
});
// If this returns false, originalName will be null, which is what we want.
nameMapping.TryGetValue(value, out string originalName);

Loading…
Cancel
Save