-
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
Allocations: Unroll AdditionalFile extension method #7652
Allocations: Unroll AdditionalFile extension method #7652
Conversation
analyzers/src/SonarAnalyzer.Common/Extensions/AnalyzerOptionsExtensions.cs
Outdated
Show resolved
Hide resolved
&& path.EndsWith(fileName, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
// The character before the filename (if there is a character) must be a directory separator | ||
if (path.Length - fileName.Length - 1 is >= 0 and var separatorPosition |
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.
There is a test case for this: The additional file ends with SonarLint.xml
but is named FooBarSonarLint.xml
.
sonar-dotnet/analyzers/tests/SonarAnalyzer.UnitTest/Common/AnalyzerConfigurationTest.cs
Lines 148 to 155 in be9ca30
public void HotspotConfiguration_GivenDifferentFileName_WillNotFinishInitialization() | |
{ | |
var sut = new HotspotConfiguration(ruleLoaderMock.Object); | |
Initialize(sut, "FooBarSonarLint.xml", "fake SonarLintXml content"); | |
ruleLoaderMock.Verify(r => r.GetEnabledRules(It.IsAny<string>()), Times.Never); | |
} |
&& separator != Path.DirectorySeparatorChar | ||
&& separator != Path.AltDirectorySeparatorChar) |
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.
We should not take any assumptions about the path separator because AdditionalFiles
is just a csc.exe command line parameter that was calculated by MSBuild.
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.
Here is what Path.GetFileName does. The span is ToString
ed by the method we call
https://github.com/dotnet/runtime/blob/2c4b4c0831d1d9f505ff5d3055c38bb41646021b/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs#L236-L248
public static ReadOnlySpan<char> GetFileName(ReadOnlySpan<char> path)
{
int root = GetPathRoot(path).Length;
// We don't want to cut off "C:\file.txt:stream" (i.e. should be "file.txt:stream")
// but we *do* want "C:Foo" => "Foo". This necessitates checking for the root.
int i = PathInternal.DirectorySeparatorChar == PathInternal.AltDirectorySeparatorChar ?
path.LastIndexOf(PathInternal.DirectorySeparatorChar) :
path.LastIndexOfAny(PathInternal.DirectorySeparatorChar, PathInternal.AltDirectorySeparatorChar);
return path.Slice(i < root ? root : i + 1);
}
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
…xtensions.cs Co-authored-by: Cristian Ambrosini <114916336+cristian-ambrosini-sonarsource@users.noreply.github.com>
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 conceptually. I've left some readability comments
analyzers/src/SonarAnalyzer.Common/Extensions/AnalyzerOptionsExtensions.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Extensions/AnalyzerOptionsExtensions.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Extensions/AnalyzerOptionsExtensions.cs
Outdated
Show resolved
Hide resolved
Kudos, SonarCloud Quality Gate passed! |
Kudos, SonarCloud Quality Gate passed! |
// HotPath: This code path needs to be allocation free. Don't use Linq. | ||
foreach (var additionalText in options.AdditionalFiles) // Uses the struct enumerator of ImmutableArray |
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.
💡 We would typically document this in Roslyn with the following attribute on the method:
[PerformanceSensitive(
"https://github.com/SonarSource/sonar-dotnet/issues/7440",
AllowCaptures = false,
AllowGenericEnumeration = false)]
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.
It should be possible to use this attribute if you install the Microsoft.CodeAnalysis.PerformanceSensitiveAnalyzers package
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.
Currently we don't have analyzer enforcement for the constraints of the attribute, but we hope to provide it in the future
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.
Fixes #7440
Tested with Akka.Net in a
dotnet build
context in a SQ EE SonaryWay run.This change safes
Path.GetFileName
The total memory allocations are down to 32,5 GB from 41,7GB = 9,2 GB savings (22%)
The number of issues reported stayed the same (3514 warnings).
Before
![image](https://private-user-images.githubusercontent.com/103252490/255585287-fb3c1302-1588-45c5-af2f-d9bfe6c720b2.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk5ODQyNjMsIm5iZiI6MTczOTk4Mzk2MywicGF0aCI6Ii8xMDMyNTI0OTAvMjU1NTg1Mjg3LWZiM2MxMzAyLTE1ODgtNDVjNS1hZjJmLWQ5YmZlNmM3MjBiMi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjE5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxOVQxNjUyNDNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT01MDYwZWQ5YmI1MDBjYjc5Y2Y4ZTQ5ZTU2ZDA5ZWI2YjJkNTc4YzQ4NzUyMjM4ODRhZmY3YmM0Yjg4N2I1Mjk1JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.EMafY3QqxuapmovxzZrpOpvbBfMturAorXOntylP4GI)
After
![image](https://private-user-images.githubusercontent.com/103252490/255585352-5979ae7a-201b-45d2-baea-f83e3b0a76ae.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRod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