Skip to content

Commit 97011f7

Browse files
Use ReadOnlySpan<char> to avoid string allocations in attribute name matching
- Use MemoryExtensions.AsSpan() to work with ReadOnlySpan<char> instead of allocating substring - Use MemoryExtensions.SequenceEqual() for span comparison - Add using System for MemoryExtensions - Reduces memory allocations when checking attribute names with parameters (e.g., @Bind:after) All tests pass (9/9 passing). Co-authored-by: DustinCampbell <[email protected]>
1 parent 03b62c1 commit 97011f7

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/UnboundDirectiveAttributeAddUsingCodeActionProvider.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Collections.Immutable;
56
using System.Diagnostics.CodeAnalysis;
67
using System.Threading;
@@ -99,11 +100,11 @@ private static bool TryGetMissingDirectiveAttributeNamespace(
99100
}
100101

101102
// For attributes with parameters (e.g., @bind:after), extract just the base attribute name
102-
var baseAttributeName = attributeName;
103-
var colonIndex = attributeName.IndexOf(':');
103+
var baseAttributeName = attributeName.AsSpan();
104+
var colonIndex = baseAttributeName.IndexOf(':');
104105
if (colonIndex > 0)
105106
{
106-
baseAttributeName = attributeName[..colonIndex];
107+
baseAttributeName = baseAttributeName[..colonIndex];
107108
}
108109

109110
// Search for matching bound attribute descriptors in all available tag helpers
@@ -119,7 +120,7 @@ private static bool TryGetMissingDirectiveAttributeNamespace(
119120
// No need to worry about multiple matches, because Razor syntax has no way to disambiguate anyway.
120121
// Currently only compiler can create directive attribute tag helpers anyway.
121122
if (boundAttribute.IsDirectiveAttribute &&
122-
boundAttribute.Name == baseAttributeName)
123+
MemoryExtensions.SequenceEqual(boundAttribute.Name.AsSpan(), baseAttributeName))
123124
{
124125
if (boundAttribute.Parent.TypeNamespace is { } typeNamespace)
125126
{

0 commit comments

Comments
 (0)