-
Notifications
You must be signed in to change notification settings - Fork 231
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 S5443 performance: Reuse compiled Regex #8183
Improve S5443 performance: Reuse compiled Regex #8183
Conversation
Kudos, SonarCloud Quality Gate passed! |
SonarCloud Quality Gate failed. 0 Bugs 100.0% Coverage Catch issues before they fail your Quality Gate with our IDE extension SonarLint |
Kudos, SonarCloud Quality Gate passed! |
Kudos, SonarCloud Quality Gate passed! |
analyzers/src/SonarAnalyzer.Common/Rules/Hotspots/PubliclyWritableDirectoriesBase.cs
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/Hotspots/PubliclyWritableDirectoriesBase.cs
Show resolved
Hide resolved
var insecureEnvironmentVariables = new[] { "tmp", "temp", "tmpdir" }; | ||
InsecureEnvironmentVariables = insecureEnvironmentVariables; | ||
UserProfile = new("""^%USERPROFILE%[\\\/]AppData[\\\/]Local[\\\/]Temp""", WindowsAndUnixOptions); | ||
LinuxDirectories = new($@"^({LinuxDirs().JoinStr("|", Regex.Escape)})(\/|$)", RegexOptions.Compiled); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would question the use of RegexOptions.Compile
in general in this rule. The cost of creating in memory assembly with emitting instructions at runtime to evaluate regex is extremely high, often not worth the benefit of the speed bump.
These regexes are generally straight-forward with not so much backtracking.
Did you try to measure the impact of the Compiled
thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not what the PR is about. The change shows significant improvements. Whether "Compiled" is bringing gains needs to be decided for all Regexes and a wide variety of projects. Create an issue, if you want us to look into it. We also have to see this in the light of the internal Regex cache: https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.cachesize?view=net-7.0#remarks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
see also #8181
Strips of another 4,5 seconds (or 0,08%) by caching the JITed regex in
IsSensitiveDirectoryUsage
:JIT
Before:
After:
Overall
Before
After