Repository: dotnet/roslyn
File: src/VisualStudio/Core/Def/InheritanceMargin/InheritanceMarginHelpers.cs (C#)

Code before:
using Microsoft.VisualStudio.Imaging.Interop;
using Microsoft.VisualStudio.LanguageServices.Implementation.InheritanceMargin.MarginGlyph;
using Roslyn.Utilities;

namespace Microsoft.VisualStudio.LanguageServices.Implementation.InheritanceMargin;

internal static class InheritanceMarginHelpers
{
    private static readonly ObjectPool<MultiDictionary<string, InheritanceTargetItem>> s_pool = new(() => []);

    /// <summary>
    /// Strips punctuation from a string for comparison purposes. Used to identify when two display names
    /// are essentially the same but differ only in punctuation (e.g., "IAliasSymbol.Target" vs "IAliasSymbolTarget").
    /// </summary>
    private static string StripPunctuation(string text)
    {
        if (string.IsNullOrEmpty(text))
            return text;

        var builder = new StringBuilder(text.Length);
        foreach (var c in text)
        {
            if (!char.IsPunctuation(c))
                builder.Append(c);
        }
        return builder.ToString();
    }

    private static readonly ImmutableArray<InheritanceRelationship> s_relationships_Shown_As_I_Up_Arrow
        =
        [
            InheritanceRelationship.ImplementedInterface,
            InheritanceRelationship.InheritedInterface,
            InheritanceRelationship.ImplementedMember,
        ];

    private static readonly ImmutableArray<InheritanceRelationship> s_relationships_Shown_As_I_Down_Arrow
        = [InheritanceRelationship.ImplementingType, InheritanceRelationship.ImplementingMember];

    private static readonly ImmutableArray<InheritanceRelationship> s_relationships_Shown_As_O_Up_Arrow
        = [InheritanceRelationship.BaseType, InheritanceRelationship.OverriddenMember];

    private static readonly ImmutableArray<InheritanceRelationship> s_relationships_Shown_As_O_Down_Arrow
        = [InheritanceRelationship.DerivedType, InheritanceRelationship.OverridingMember];

    /// <summary>
    /// Decide which moniker should be shown.
    /// </summary>
    public static ImageMoniker GetMoniker(InheritanceRelationship inheritanceRelationship)
    {
        //  If there are multiple targets and we have the corresponding compound image, use it

Review comment (suggestion):
@copilot if there is no punctuation encountered, then return the original text.  That way we avoid unnecessary allocs in teh common case.

Code after:
using Microsoft.VisualStudio.LanguageServices.Implementation.InheritanceMargin.MarginGlyph;
using Roslyn.Utilities;

namespace Microsoft.VisualStudio.LanguageServices.Implementation.InheritanceMargin;

internal static class InheritanceMarginHelpers
{
    private static readonly ObjectPool<MultiDictionary<string, InheritanceTargetItem>> s_pool = new(() => []);

    /// <summary>
    /// Strips punctuation from a string for comparison purposes. Used to identify when two display names
    /// are essentially the same but differ only in punctuation (e.g., "IAliasSymbol.Target" vs "IAliasSymbolTarget").
    /// </summary>
    private static string StripPunctuation(string text)
    {
        using var _ = PooledStringBuilder.GetInstance(out var builder);
        var hasPunctuation = false;
        foreach (var c in text)
        {
            if (char.IsPunctuation(c))
            {
                hasPunctuation = true;
            }
            else
            {
                builder.Append(c);
            }
        }

        return hasPunctuation ? builder.ToString() : text;
    }

    private static readonly ImmutableArray<InheritanceRelationship> s_relationships_Shown_As_I_Up_Arrow
        =
        [
            InheritanceRelationship.ImplementedInterface,
            InheritanceRelationship.InheritedInterface,
            InheritanceRelationship.ImplementedMember,
        ];

    private static readonly ImmutableArray<InheritanceRelationship> s_relationships_Shown_As_I_Down_Arrow
        = [InheritanceRelationship.ImplementingType, InheritanceRelationship.ImplementingMember];

    private static readonly ImmutableArray<InheritanceRelationship> s_relationships_Shown_As_O_Up_Arrow
        = [InheritanceRelationship.BaseType, InheritanceRelationship.OverriddenMember];

    private static readonly ImmutableArray<InheritanceRelationship> s_relationships_Shown_As_O_Down_Arrow
        = [InheritanceRelationship.DerivedType, InheritanceRelationship.OverridingMember];

    /// <summary>
    /// Decide which moniker should be shown.