|
| 1 | +using System.Collections.Concurrent; |
1 | 2 | using System.Collections.Immutable;
|
2 | 3 | using DynamoDBGenerator.SourceGenerator.Types;
|
3 | 4 | using Microsoft.CodeAnalysis;
|
@@ -37,17 +38,16 @@ static TypeIdentifier Create(ITypeSymbol typeSymbol)
|
37 | 38 | }
|
38 | 39 | }
|
39 | 40 |
|
40 |
| - private static readonly Dictionary<ITypeSymbol, (string, string)> RepresentationDictionary = |
| 41 | + private static readonly ConcurrentDictionary<ITypeSymbol, (string, string)> RepresentationDictionary = |
41 | 42 | new(SymbolEqualityComparer.IncludeNullability);
|
42 | 43 |
|
43 | 44 | public static (string annotated, string original) Representation(this ITypeSymbol typeSymbol)
|
44 | 45 | {
|
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 | + }); |
51 | 51 |
|
52 | 52 | static string ToString(ITypeSymbol x, string displayString)
|
53 | 53 | {
|
@@ -111,27 +111,17 @@ static string ExceptionMessage(ISymbol typeSymbol) =>
|
111 | 111 | public static Func<ITypeSymbol, string> SuffixedTypeSymbolNameFactory(string? suffix,
|
112 | 112 | IEqualityComparer<ISymbol?> comparer)
|
113 | 113 | {
|
114 |
| - var dict = new Dictionary<ITypeSymbol, string>(comparer); |
| 114 | + var dict = new ConcurrentDictionary<ITypeSymbol, string>(comparer); |
115 | 115 |
|
116 | 116 | Func<ITypeSymbol, string> implementation;
|
117 | 117 | 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}"); |
121 | 119 | 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 | + ); |
135 | 125 |
|
136 | 126 | return implementation;
|
137 | 127 |
|
|
0 commit comments