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

Improve Rule S2223: cleanup and performance #6761

Merged
merged 14 commits into from Feb 22, 2023

Conversation

mary-georgiou-sonarsource
Copy link
Contributor

RSPEC PR: SonarSource/rspec#1583

Performance comparison:
pascalabcnet and servuo were the two projects with most issues for rule S2223 on peach.

Project Before After Performance Improvement
pascalabcnet 544 issues - 1,694 s 544 issues - 1,262ms -25.5%
servuo 437 issues - 4,305 s 437 issues - 2,988ms -30.5%

@andrei-epure-sonarsource
Copy link
Contributor

I suggest to test the performance impact (before and after) - see this page and/or the msbuild -p:reportanalyzer=true parameter

To get the AnalyzerRunner, you need to build this project locally.

Copy link
Contributor

@antonioaversa antonioaversa left a comment

Choose a reason for hiding this comment

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

Minor comments.

The only main aspect is the private protected visibility, which would possibly (but not necessarily) be a false negative, since this visibility is almost private as private itself.

@github-actions github-actions bot moved this from Review in progress to In progress in Best Kanban Feb 21, 2023
node.Modifiers.Count > 1
&& node.Modifiers.Any(SyntaxKind.StaticKeyword)
&& (!node.Modifiers.Any(SyntaxKind.PrivateKeyword) || (node.Modifiers.Any(SyntaxKind.PrivateKeyword) && node.Modifiers.Any(SyntaxKind.ProtectedKeyword)))
&& !node.Modifiers.Any(SyntaxKind.ReadOnlyKeyword);

Choose a reason for hiding this comment

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

A volatile field should also be irrelevant as volatile indicates some advanced multi-threading voodoo is going on here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe good to exclude - per MSDN docs

The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time.

On a multiprocessor system, a volatile read operation does not guarantee to obtain the latest value written to that memory location by any processor.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, while I see a public static volatile non-readonly field as dangerous as a public static non-volatile non-readonly one, and it seems that there are almost always ways to have the field private (at least for the typical scenarios of sentinel loopers or counters), let's assume that the person using it has a sound multi-threading model and a solid understanding of it.

So I am OK with disabling the check when volatile is encountered.

Copy link
Contributor

Choose a reason for hiding this comment

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

@martin-strecker-sonarsource to resolve this one

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@martin-strecker-sonarsource and I had an offiline convertation on the matter and agreed to the current solution regarding volatile modifier.

@andrei-epure-sonarsource andrei-epure-sonarsource removed their request for review February 22, 2023 08:39
@mary-georgiou-sonarsource
Copy link
Contributor Author

@antonioaversa I added also the volatile keyword as an exception as from the msdn docs my understanding is that when a field is declared volatile it is expected that the values might change in an unexpected way and might be an FP?

On a multiprocessor system, a volatile read operation does not guarantee to obtain the latest value written to that memory location by any processor.

@mary-georgiou-sonarsource mary-georgiou-sonarsource marked this pull request as ready for review February 22, 2023 12:38
Copy link
Contributor

@antonioaversa antonioaversa left a comment

Choose a reason for hiding this comment

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

LGTM! Just minor things, mostly aesthetics.

protected static double Pi4 = 3.14; // Noncompliant
internal static double Pi5 = 3.14; // Noncompliant
internal static double Pi6 = 3.14; // Noncompliant
protected internal static double Pi7 = 3.14; // Noncompliant
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be good to align vertically all these // Noncompliant.

node.Modifiers.Count > 1
&& node.Modifiers.Any(SyntaxKind.StaticKeyword)
&& (!node.Modifiers.Any(SyntaxKind.PrivateKeyword) || (node.Modifiers.Any(SyntaxKind.PrivateKeyword) && node.Modifiers.Any(SyntaxKind.ProtectedKeyword)))
&& !node.Modifiers.Any(SyntaxKind.ReadOnlyKeyword);
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, while I see a public static volatile non-readonly field as dangerous as a public static non-volatile non-readonly one, and it seems that there are almost always ways to have the field private (at least for the typical scenarios of sentinel loopers or counters), let's assume that the person using it has a sound multi-threading model and a solid understanding of it.

So I am OK with disabling the check when volatile is encountered.

Best Kanban automation moved this from Review in progress to Review approved Feb 22, 2023
@mary-georgiou-sonarsource mary-georgiou-sonarsource added this to the 8.54 milestone Feb 22, 2023
@sonarcloud
Copy link

sonarcloud bot commented Feb 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

@sonarcloud
Copy link

sonarcloud bot commented Feb 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

@mary-georgiou-sonarsource mary-georgiou-sonarsource merged commit 55a4d46 into master Feb 22, 2023
Best Kanban automation moved this from Review approved to Validate Peach Feb 22, 2023
@mary-georgiou-sonarsource mary-georgiou-sonarsource deleted the mary/S2223-hardening branch February 22, 2023 15:19
@mary-georgiou-sonarsource mary-georgiou-sonarsource moved this from Validate Peach to Done in Best Kanban Feb 24, 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

4 participants