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 CFG memory allocations #5402

Merged
merged 2 commits into from
Feb 18, 2022
Merged

Conversation

pavel-mikula-sonarsource
Copy link
Contributor

Fixes #5401

@pavel-mikula-sonarsource
Copy link
Contributor Author

I'm not going to fix the coverage issue, as the constructor is private and currently only reachable through Wrap method.

Base automatically changed from Pavel/ReduceMemory to master February 17, 2022 16:45
internal static class PropertyInfoExtensions
{
public static T ReadCached<T>(this PropertyInfo property, object instance, ref T cache) where T : class =>
cache ??= (T)property.GetValue(instance);
Copy link
Member

Choose a reason for hiding this comment

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

This will have the downside that, if a value is null, we will continue to ask for it, even if we requested it before. As long as this method proves better in practice, this is not a problem but we should be aware of it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I considered that downside. For current usage, the BasicBlock.BranchValue is the hottest candidate to be null. And I assume it's going to be called 2 times per instance (once through OperationsAndBranchValue that cases the result, once for branching in the future).
The rest should be called zero or once (OriginalOperation, ExceptionType, Parent), or not be null (Children, Language, SemanticModel).

public ControlFlowConditionKind ConditionKind => ConditionKindProperty.ReadCached(instance, ref conditionKind);
public ControlFlowRegion EnclosingRegion => EnclosingRegionProperty.ReadCached(instance, ControlFlowRegion.Wrap, ref enclosingRegion);
public ControlFlowBranch FallThroughSuccessor => FallThroughSuccessorProperty.ReadCached(instance, ControlFlowBranch.Wrap, ref fallThroughSuccessor);
public bool IsReachable => IsReachableProperty.ReadCached<bool>(instance, ref isReachable);

Choose a reason for hiding this comment

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

<bool> is not needed

public ControlFlowRegion EnclosingRegion => EnclosingRegionProperty.ReadCached(instance, ControlFlowRegion.Wrap, ref enclosingRegion);
public ControlFlowBranch FallThroughSuccessor => FallThroughSuccessorProperty.ReadCached(instance, ControlFlowBranch.Wrap, ref fallThroughSuccessor);
public bool IsReachable => IsReachableProperty.ReadCached<bool>(instance, ref isReachable);
public BasicBlockKind Kind => KindProperty.ReadCached<BasicBlockKind>(instance, ref kind);

Choose a reason for hiding this comment

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

<BasicBlockKind> is not needed

public bool IsReachable => IsReachableProperty.ReadCached<bool>(instance, ref isReachable);
public BasicBlockKind Kind => KindProperty.ReadCached<BasicBlockKind>(instance, ref kind);
public ImmutableArray<IOperation> Operations => OperationsProperty.ReadCached(instance, ref operations);
public int Ordinal => OrdinalProperty.ReadCached<int>(instance, ref ordinal);

Choose a reason for hiding this comment

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

<int> is not needed

Copy link
Member

Choose a reason for hiding this comment

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

LGTM! there are a few minor comments

@pavel-mikula-sonarsource pavel-mikula-sonarsource linked an issue Feb 18, 2022 that may be closed by this pull request
@pavel-mikula-sonarsource pavel-mikula-sonarsource marked this pull request as ready for review February 18, 2022 07:48
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarqubecloud
Copy link

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

94.1% 94.1% Coverage
0.0% 0.0% Duplication

@pavel-mikula-sonarsource pavel-mikula-sonarsource merged commit f1f160f into master Feb 18, 2022
@pavel-mikula-sonarsource pavel-mikula-sonarsource deleted the Pavel/ReduceMemory-CFG branch February 18, 2022 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reduce CFG memory allocations
2 participants