Skip to content

Commit

Permalink
Optimize namespace comparisons
Browse files Browse the repository at this point in the history
Fixes: #16922

Improves the performance significantly by avoiding allocations for the
purpose of comparing the namespace.
  • Loading branch information
rynowak committed Nov 15, 2019
1 parent 2395121 commit 4fa5f0b
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Immutable;
using Microsoft.AspNetCore.Components.Analyzers;
using Microsoft.CodeAnalysis;
Expand All @@ -15,6 +16,8 @@ namespace Microsoft.Extensions.Internal
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class ComponentInternalUsageDiagnosticAnalyzer : DiagnosticAnalyzer
{
private static readonly string[] NamespaceParts = new[] { "RenderTree", "Components", "AspNetCore", "Microsoft", };

private readonly InternalUsageAnalyzer _inner;

public ComponentInternalUsageDiagnosticAnalyzer()
Expand All @@ -27,17 +30,25 @@ public ComponentInternalUsageDiagnosticAnalyzer()

public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);

_inner.Register(context);
}

private static bool IsInInternalNamespace(ISymbol symbol)
{
if (symbol?.ContainingNamespace?.ToDisplayString() is string ns)
var @namespace = symbol?.ContainingNamespace;
for (var i = 0; i < NamespaceParts.Length; i++)
{
return string.Equals(ns, "Microsoft.AspNetCore.Components.RenderTree");
if (@namespace == null || !string.Equals(NamespaceParts[i], @namespace.Name, StringComparison.Ordinal))
{
return false;
}

@namespace = @namespace.ContainingNamespace;
}

return false;
return true;
}
}
}

0 comments on commit 4fa5f0b

Please sign in to comment.