Skip to content

Commit 1dd5ced

Browse files
authored
Merge pull request #3711 from martin-strecker-sonarsource/AllocationsInCanWrap
Avoid allocations in CanWrap... methods
2 parents 3411684 + 12b6f93 commit 1dd5ced

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/LightupHelpers.cs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,13 @@ internal static bool CanWrapObject(object obj, Type underlyingType)
6565
return false;
6666
}
6767

68-
ConcurrentDictionary<Type, bool> wrappedObject = SupportedObjectWrappers.GetOrAdd(underlyingType, _ => new ConcurrentDictionary<Type, bool>());
68+
ConcurrentDictionary<Type, bool> wrappedObject = SupportedObjectWrappers.GetOrAdd(underlyingType, static _ => new ConcurrentDictionary<Type, bool>());
6969

70-
// Avoid creating the delegate if the value already exists
71-
bool canCast;
72-
if (!wrappedObject.TryGetValue(obj.GetType(), out canCast))
70+
// Avoid creating a delegate and capture class
71+
if (!wrappedObject.TryGetValue(obj.GetType(), out var canCast))
7372
{
74-
canCast = wrappedObject.GetOrAdd(
75-
obj.GetType(),
76-
kind => underlyingType.GetTypeInfo().IsAssignableFrom(obj.GetType().GetTypeInfo()));
73+
canCast = underlyingType.GetTypeInfo().IsAssignableFrom(obj.GetType().GetTypeInfo());
74+
wrappedObject.TryAdd(obj.GetType(), canCast);
7775
}
7876

7977
return canCast;
@@ -93,15 +91,13 @@ internal static bool CanWrapNode(SyntaxNode node, Type underlyingType)
9391
return false;
9492
}
9593

96-
ConcurrentDictionary<SyntaxKind, bool> wrappedSyntax = SupportedSyntaxWrappers.GetOrAdd(underlyingType, _ => new ConcurrentDictionary<SyntaxKind, bool>());
94+
ConcurrentDictionary<SyntaxKind, bool> wrappedSyntax = SupportedSyntaxWrappers.GetOrAdd(underlyingType, static _ => new ConcurrentDictionary<SyntaxKind, bool>());
9795

98-
// Avoid creating the delegate if the value already exists
99-
bool canCast;
100-
if (!wrappedSyntax.TryGetValue(node.Kind(), out canCast))
96+
// Avoid creating a delegate and capture class
97+
if (!wrappedSyntax.TryGetValue(node.Kind(), out var canCast))
10198
{
102-
canCast = wrappedSyntax.GetOrAdd(
103-
node.Kind(),
104-
kind => underlyingType.GetTypeInfo().IsAssignableFrom(node.GetType().GetTypeInfo()));
99+
canCast = underlyingType.GetTypeInfo().IsAssignableFrom(node.GetType().GetTypeInfo());
100+
wrappedSyntax.TryAdd(node.Kind(), canCast);
105101
}
106102

107103
return canCast;
@@ -121,15 +117,13 @@ internal static bool CanWrapOperation(IOperation operation, Type underlyingType)
121117
return false;
122118
}
123119

124-
ConcurrentDictionary<OperationKind, bool> wrappedSyntax = SupportedOperationWrappers.GetOrAdd(underlyingType, _ => new ConcurrentDictionary<OperationKind, bool>());
120+
ConcurrentDictionary<OperationKind, bool> wrappedSyntax = SupportedOperationWrappers.GetOrAdd(underlyingType, static _ => new ConcurrentDictionary<OperationKind, bool>());
125121

126-
// Avoid creating the delegate if the value already exists
127-
bool canCast;
128-
if (!wrappedSyntax.TryGetValue(operation.Kind, out canCast))
122+
// Avoid creating a delegate and capture class
123+
if (!wrappedSyntax.TryGetValue(operation.Kind, out var canCast))
129124
{
130-
canCast = wrappedSyntax.GetOrAdd(
131-
operation.Kind,
132-
kind => underlyingType.GetTypeInfo().IsAssignableFrom(operation.GetType().GetTypeInfo()));
125+
canCast = underlyingType.GetTypeInfo().IsAssignableFrom(operation.GetType().GetTypeInfo());
126+
wrappedSyntax.TryAdd(operation.Kind, canCast);
133127
}
134128

135129
return canCast;

0 commit comments

Comments
 (0)