Skip to content

Commit 3199a24

Browse files
committed
Use ConcurrentDictionary where applicable
1 parent 7e2128b commit 3199a24

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

src/DynamoDBGenerator.SourceGenerator/Extensions/TypeExtensions.cs

+14-24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Concurrent;
12
using System.Collections.Immutable;
23
using DynamoDBGenerator.SourceGenerator.Types;
34
using Microsoft.CodeAnalysis;
@@ -37,17 +38,16 @@ static TypeIdentifier Create(ITypeSymbol typeSymbol)
3738
}
3839
}
3940

40-
private static readonly Dictionary<ITypeSymbol, (string, string)> RepresentationDictionary =
41+
private static readonly ConcurrentDictionary<ITypeSymbol, (string, string)> RepresentationDictionary =
4142
new(SymbolEqualityComparer.IncludeNullability);
4243

4344
public static (string annotated, string original) Representation(this ITypeSymbol typeSymbol)
4445
{
45-
if (RepresentationDictionary.TryGetValue(typeSymbol, out var res))
46-
return res;
47-
48-
var displayString = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
49-
50-
return RepresentationDictionary[typeSymbol] = (ToString(typeSymbol, displayString), displayString);
46+
return RepresentationDictionary.GetOrAdd(typeSymbol, x =>
47+
{
48+
var displayString = x.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
49+
return RepresentationDictionary[typeSymbol] = (ToString(typeSymbol, displayString), displayString);
50+
});
5151

5252
static string ToString(ITypeSymbol x, string displayString)
5353
{
@@ -111,27 +111,17 @@ static string ExceptionMessage(ISymbol typeSymbol) =>
111111
public static Func<ITypeSymbol, string> SuffixedTypeSymbolNameFactory(string? suffix,
112112
IEqualityComparer<ISymbol?> comparer)
113113
{
114-
var dict = new Dictionary<ITypeSymbol, string>(comparer);
114+
var dict = new ConcurrentDictionary<ITypeSymbol, string>(comparer);
115115

116116
Func<ITypeSymbol, string> implementation;
117117
if (Equals(comparer, SymbolEqualityComparer.IncludeNullability))
118-
{
119-
implementation = x => dict.TryGetValue(x, out var res) ? res : dict[x] = $"{NullableAnnotation(x)}{suffix}";
120-
}
118+
implementation = x => dict.GetOrAdd(x, y => $"{NullableAnnotation(y)}{suffix}");
121119
else
122-
{
123-
implementation = x =>
124-
{
125-
if (dict.TryGetValue(x, out var res))
126-
return res;
127-
128-
var displayString = x.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);
129-
// Could cause a NullReference exception but very unlikely since all IArrayTypeSymbol should inherit from Array.
130-
return dict[x] = x is IArrayTypeSymbol
131-
? $"{displayString}_{x.BaseType!.ToDisplayString()}{suffix}"
132-
: $"{displayString.ToAlphaNumericMethodName()}{suffix}";
133-
};
134-
}
120+
implementation = x => dict.GetOrAdd(x,
121+
y => y is IArrayTypeSymbol
122+
? $"{y.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)}_{y.BaseType!.ToDisplayString()}{suffix}"
123+
: $"{y.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat).ToAlphaNumericMethodName()}{suffix}"
124+
);
135125

136126
return implementation;
137127

0 commit comments

Comments
 (0)