Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid allocations in LightupHelpers.CanWrap... methods #8107

Merged
merged 4 commits into from
Oct 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions analyzers/src/SonarAnalyzer.CFG/ShimLayer/LightupHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.CodeAnalysis.CSharp;
using Roslyn.Utilities;

namespace StyleCop.Analyzers.Lightup;

Expand Down Expand Up @@ -39,6 +40,7 @@ private static readonly ConcurrentDictionary<Type, ConcurrentDictionary<Operatio

public static bool SupportsIOperation => SupportsCSharp73;

[PerformanceSensitive("https://github.com/SonarSource/sonar-dotnet/issues/8106", AllowCaptures = false, AllowGenericEnumeration = false, AllowImplicitBoxing = false)]
internal static bool CanWrapObject(object obj, Type underlyingType)
{
if (obj == null)
Expand All @@ -53,20 +55,19 @@ internal static bool CanWrapObject(object obj, Type underlyingType)
return false;
}

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

// Avoid creating the delegate if the value already exists
bool canCast;
if (!wrappedObject.TryGetValue(obj.GetType(), out canCast))
// Avoid creating a delegate and capture class
if (!wrappedObject.TryGetValue(obj.GetType(), out var canCast))
{
canCast = wrappedObject.GetOrAdd(
obj.GetType(),
kind => underlyingType.GetTypeInfo().IsAssignableFrom(obj.GetType().GetTypeInfo()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was very weird... the key kind was not used at all...

canCast = underlyingType.GetTypeInfo().IsAssignableFrom(obj.GetType().GetTypeInfo());
wrappedObject.TryAdd(obj.GetType(), canCast);
}

return canCast;
}

[PerformanceSensitive("https://github.com/SonarSource/sonar-dotnet/issues/8106", AllowCaptures = false, AllowGenericEnumeration = false, AllowImplicitBoxing = false)]
internal static bool CanWrapNode(SyntaxNode node, Type underlyingType)
{
if (node == null)
Expand All @@ -81,20 +82,19 @@ internal static bool CanWrapNode(SyntaxNode node, Type underlyingType)
return false;
}

ConcurrentDictionary<SyntaxKind, bool> wrappedSyntax = SupportedSyntaxWrappers.GetOrAdd(underlyingType, _ => new ConcurrentDictionary<SyntaxKind, bool>());
ConcurrentDictionary<SyntaxKind, bool> wrappedSyntax = SupportedSyntaxWrappers.GetOrAdd(underlyingType, static _ => new ConcurrentDictionary<SyntaxKind, bool>());

// Avoid creating the delegate if the value already exists
bool canCast;
if (!wrappedSyntax.TryGetValue(node.Kind(), out canCast))
// Avoid creating a delegate and capture class
if (!wrappedSyntax.TryGetValue(node.Kind(), out var canCast))
{
canCast = wrappedSyntax.GetOrAdd(
node.Kind(),
kind => underlyingType.GetTypeInfo().IsAssignableFrom(node.GetType().GetTypeInfo()));
canCast = underlyingType.GetTypeInfo().IsAssignableFrom(node.GetType().GetTypeInfo());
wrappedSyntax.TryAdd(node.Kind(), canCast);
}

return canCast;
}

[PerformanceSensitive("https://github.com/SonarSource/sonar-dotnet/issues/8106", AllowCaptures = false, AllowGenericEnumeration = false, AllowImplicitBoxing = false)]
internal static bool CanWrapOperation(IOperation operation, Type underlyingType)
{
if (operation == null)
Expand All @@ -109,15 +109,13 @@ internal static bool CanWrapOperation(IOperation operation, Type underlyingType)
return false;
}

ConcurrentDictionary<OperationKind, bool> wrappedSyntax = SupportedOperationWrappers.GetOrAdd(underlyingType, _ => new ConcurrentDictionary<OperationKind, bool>());
ConcurrentDictionary<OperationKind, bool> wrappedSyntax = SupportedOperationWrappers.GetOrAdd(underlyingType, static _ => new ConcurrentDictionary<OperationKind, bool>());

// Avoid creating the delegate if the value already exists
bool canCast;
if (!wrappedSyntax.TryGetValue(operation.Kind, out canCast))
// Avoid creating a delegate and capture class
if (!wrappedSyntax.TryGetValue(operation.Kind, out var canCast))
{
canCast = wrappedSyntax.GetOrAdd(
operation.Kind,
kind => underlyingType.GetTypeInfo().IsAssignableFrom(operation.GetType().GetTypeInfo()));
canCast = underlyingType.GetTypeInfo().IsAssignableFrom(operation.GetType().GetTypeInfo());
wrappedSyntax.TryAdd(operation.Kind, canCast);
}

return canCast;
Expand Down