Skip to content

Commit 916b9a6

Browse files
Merge pull request #6562 from dotnet/charArrayAllocs
Reduce allocations more in public api analyzer
2 parents 9a0d72e + 20ae623 commit 916b9a6

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/PublicApiAnalyzers/Core/Analyzers/DeclarePublicApiAnalyzer.Impl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ string getApiString(ISymbol symbol, SymbolDisplayFormat format)
586586

587587
private static bool ContainsPublicApiName(string apiLineText, string publicApiNameToSearch)
588588
{
589-
apiLineText = apiLineText.Trim(ObliviousMarker);
589+
apiLineText = apiLineText.TrimStart(ObliviousMarkerArray);
590590

591591
// Ensure we don't search in parameter list/return type.
592592
var indexOfParamsList = apiLineText.IndexOf('(');

src/PublicApiAnalyzers/Core/Analyzers/DeclarePublicApiAnalyzer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public sealed partial class DeclarePublicApiAnalyzer : DiagnosticAnalyzer
4949
internal const string FileName = "FileName";
5050

5151
private const char ObliviousMarker = '~';
52+
private static readonly char[] ObliviousMarkerArray = { ObliviousMarker };
5253

5354
/// <summary>
5455
/// Boolean option to configure if public API analyzer should bail out silently if public API files are missing.
@@ -397,7 +398,7 @@ private static bool ValidateApiFiles(ApiData shippedData, ApiData unshippedData,
397398
errors.Add(Diagnostic.Create(descriptor, Location.None, InvalidReasonMisplacedNullableEnable));
398399
}
399400

400-
var publicApiMap = new Dictionary<string, ApiLine>(StringComparer.Ordinal);
401+
using var publicApiMap = PooledDictionary<string, ApiLine>.GetInstance(StringComparer.Ordinal);
401402
ValidateApiList(publicApiMap, shippedData.ApiList, isPublic, errors);
402403
ValidateApiList(publicApiMap, unshippedData.ApiList, isPublic, errors);
403404

@@ -408,7 +409,7 @@ private static void ValidateApiList(Dictionary<string, ApiLine> publicApiMap, Im
408409
{
409410
foreach (ApiLine cur in apiList)
410411
{
411-
string textWithoutOblivious = cur.Text.TrimStart(ObliviousMarker);
412+
string textWithoutOblivious = cur.Text.TrimStart(ObliviousMarkerArray);
412413
if (publicApiMap.TryGetValue(textWithoutOblivious, out ApiLine existingLine))
413414
{
414415
LinePositionSpan existingLinePositionSpan = existingLine.SourceText.Lines.GetLinePositionSpan(existingLine.Span);

0 commit comments

Comments
 (0)