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

Reduce IOperationWrapperSonar memory allocations #5400

Merged
merged 1 commit into from
Feb 17, 2022
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
29 changes: 11 additions & 18 deletions analyzers/src/SonarAnalyzer.CFG/ShimLayer/IOperationWrapperSonar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ public class IOperationWrapperSonar
private static readonly PropertyInfo IsImplicitProperty;
private static readonly PropertyInfo SemanticModelProperty;

private readonly Lazy<IOperation> parent;
private readonly Lazy<IEnumerable<IOperation>> children;
private readonly Lazy<string> language;
private readonly Lazy<bool> isImplicit;
private readonly Lazy<SemanticModel> semanticModel;
private IOperation parent;
private IEnumerable<IOperation> children;
private string language;
private bool? isImplicit;
private SemanticModel semanticModel;

public IOperation Instance { get; }
public IOperation Parent => parent.Value;
public IEnumerable<IOperation> Children => children.Value;
public string Language => language.Value;
public bool IsImplicit => isImplicit.Value;
public SemanticModel SemanticModel => semanticModel.Value;
public IOperation Parent => parent ??= (IOperation)ParentProperty.GetValue(Instance);
public IEnumerable<IOperation> Children => children ??= (IEnumerable<IOperation>)ChildrenProperty.GetValue(Instance);
public string Language => language ??= (string)LanguageProperty.GetValue(Instance);
public bool IsImplicit => isImplicit ??= (bool)IsImplicitProperty.GetValue(Instance);
public SemanticModel SemanticModel => semanticModel ??= (SemanticModel)SemanticModelProperty.GetValue(Instance);

static IOperationWrapperSonar()
{
Expand All @@ -58,15 +58,8 @@ static IOperationWrapperSonar()
SemanticModelProperty = type.GetProperty(nameof(SemanticModel));
}

public IOperationWrapperSonar(IOperation instance)
{
public IOperationWrapperSonar(IOperation instance) =>
Instance = instance ?? throw new ArgumentNullException(nameof(instance));
parent = ParentProperty.ReadValue<IOperation>(instance);
children = ChildrenProperty.ReadValue<IEnumerable<IOperation>>(instance);
language = LanguageProperty.ReadValue<string>(instance);
isImplicit = IsImplicitProperty.ReadValue<bool>(instance);
semanticModel = SemanticModelProperty.ReadValue<SemanticModel>(instance);
}

public override int GetHashCode() =>
Instance.GetHashCode();
Expand Down