Skip to content

Commit

Permalink
Using ArrayPool to avoid array allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingzhe Huang (from Dev Box) committed Oct 25, 2023
1 parent 3dbe78d commit 15faf32
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System;
using System.Buffers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

Expand All @@ -13,8 +14,7 @@ internal class AnalyzerUtils

internal static bool IsSdkCode(ISymbol symbol)
{
var namespaces = symbol.ContainingNamespace.GetAllNamespaces();

using var namespaces = symbol.ContainingNamespace.GetAllNamespaces();
return IsSdkNamespace(namespaces);
}

Expand All @@ -28,15 +28,15 @@ internal static bool IsSdkCode(SyntaxNode node, SemanticModel model)
return IsSdkCode(symbol);
}

var namespaces = GetNamespace(node);
using var namespaces = GetNamespace(node);
return IsSdkNamespace(namespaces);
}

private static bool IsSdkNamespace(IReadOnlyList<string> namespaces) => namespaces.Count >= 2 && namespaces[0] == "Azure" && namespaces[1] != "Core";
private static bool IsSdkNamespace(Namespaces namespaces) => namespaces.Count >= 2 && namespaces[0] == "Azure" && namespaces[1] != "Core";

private static IReadOnlyList<string> GetNamespace(SyntaxNode node)
private static Namespaces GetNamespace(SyntaxNode node)
{
var namespaces = new List<string>();
var namespaces = new Namespaces();

var parent = node.Parent;

Expand All @@ -63,7 +63,37 @@ parent is not NamespaceDeclarationSyntax
}
}

namespaces.Reverse();

return namespaces;
}

internal class Namespaces : IDisposable
{
private int count;
private readonly string[] namespaces = ArrayPool<string>.Shared.Rent(10);

public int Count => this.count;

public void Add(string name)
{
this.namespaces[this.count++] = name;
}

public string this[int i]
{
get => this.namespaces[i];
}

public void Reverse()
{
Array.Reverse(this.namespaces, 0, this.count);
}

public void Dispose()
{
ArrayPool<string>.Shared.Return(this.namespaces);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Text;
using Microsoft.CodeAnalysis;
using static Azure.ClientSdk.Analyzers.AnalyzerUtils;

namespace Azure.ClientSdk.Analyzers
{
Expand All @@ -24,9 +24,9 @@ public static StringBuilder GetFullNamespaceName(this INamespaceSymbol namespace
return namespaceName;
}

internal static IReadOnlyList<string> GetAllNamespaces(this INamespaceSymbol namespaceSymbol)
internal static Namespaces GetAllNamespaces(this INamespaceSymbol namespaceSymbol)
{
var namespaces = new List<string>();
var namespaces = new Namespaces();
while (namespaceSymbol is { IsGlobalNamespace: false })
{
namespaces.Add(namespaceSymbol.Name);
Expand Down

0 comments on commit 15faf32

Please sign in to comment.