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

NullPointerDereference.SyntaxKindWalker reduce allocations and evaluations in the hot path #8385

Merged

Conversation

martin-strecker-sonarsource
Copy link
Contributor

See also #8103

This PR fixes the params array allocation caused by IsAnyKind and reactors the SyntaxWalker to do the SyntaxKind checks in the dedicated Visit... methods. That way the number of comparisons is reduced.

Copy link
Contributor

@pavel-mikula-sonarsource pavel-mikula-sonarsource left a comment

Choose a reason for hiding this comment

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

LGTM, I left some polishing comments


public override void VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
{
if (node.Kind() is SyntaxKind.SimpleMemberAccessExpression)
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not really a pattern to match

Suggested change
if (node.Kind() is SyntaxKind.SimpleMemberAccessExpression)
if (node.Kind() == SyntaxKind.SimpleMemberAccessExpression)

{
public bool Result { get; private set; }

public override void Visit(SyntaxNode node)
{
Result |= node.Kind() is SyntaxKindEx.ForEachVariableStatement;
Copy link
Contributor

Choose a reason for hiding this comment

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

It's simple int comparison

Suggested change
Result |= node.Kind() is SyntaxKindEx.ForEachVariableStatement;
Result |= node.Kind() == SyntaxKindEx.ForEachVariableStatement;

{
public bool Result { get; private set; }

public override void Visit(SyntaxNode node)
{
Result |= node.Kind() is SyntaxKindEx.ForEachVariableStatement;
Copy link
Contributor

Choose a reason for hiding this comment

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

That's so annoying that it's a different syntax kind :/ We're likely missing it all over the place

{
public unsafe void M(int i)
{
int* iPtr = &i;
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use int* iPtr argument directly, to avoid this scaffolding.

}

[DataTestMethod]
[DataRow("""Dim i As System.Int32""")]
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't need to be a DataTestMethod. Or you can add this, to make it similar to the C# version

[DataRow("""Dim S As String = (ToString()?.Length)?.ToString()""")]

public class NullPointerDereferenceSyntaxKindWalkerTests
{
[DataTestMethod]
[DataRow("""await Task.Yield();""")]
Copy link
Contributor

Choose a reason for hiding this comment

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

None of the DataRow needs """. Simple string is enough in all cases

@github-actions github-actions bot moved this from Review in progress to Review approved in Best Kanban Nov 21, 2023
Copy link

sonarcloud bot commented Nov 22, 2023

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

Copy link

sonarcloud bot commented Nov 22, 2023

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

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@martin-strecker-sonarsource martin-strecker-sonarsource merged commit f9bf68c into master Nov 23, 2023
32 checks passed
Best Kanban automation moved this from Review approved to Validate Peach Nov 23, 2023
@martin-strecker-sonarsource martin-strecker-sonarsource deleted the Martin/NullPointerDereference_SyntaxWalker branch November 23, 2023 15:48
@martin-strecker-sonarsource martin-strecker-sonarsource moved this from Validate Peach to Done in Best Kanban Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Best Kanban
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants