-
Notifications
You must be signed in to change notification settings - Fork 240
Reduce allocations in TagHelperBinder.ProcessDescriptors #12237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ToddGrun
merged 7 commits into
dotnet:main
from
ToddGrun:dev/toddgrun/TagHelperBinder.ProcessDescriptors-Allocations
Sep 23, 2025
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1b434c6
*** WIP: Reduce allocations in TagHelperBinder.ProcessDescriptors
ToddGrun 9338f1a
Switch to Dustin's approach
ToddGrun 1e334fd
Fix issue exposed by tests, some code cleanup
ToddGrun 2a506b8
Add compiler benchmark for TagHelperBinder construction
DustinCampbell 27b7524
Improve TagHelperBinder.ProcessDescriptors (version 3)
DustinCampbell 85c0906
Remove unnecessary EnsureCapacity call
ToddGrun a61e5b6
PR suggestions
ToddGrun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
...mpiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/TagHelperBinder.TagHelperSet.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Diagnostics; | ||
|
|
||
| namespace Microsoft.AspNetCore.Razor.Language; | ||
|
|
||
| internal sealed partial class TagHelperBinder | ||
| { | ||
| /// <summary> | ||
| /// Similar to <see cref="ImmutableArray{T}"/>, but optimized to store either a single value or an array of values. | ||
| /// </summary> | ||
| [DebuggerDisplay("{GetDebuggerDisplay(),nq}")] | ||
| [DebuggerTypeProxy(typeof(DebuggerProxy))] | ||
| private readonly struct TagHelperSet | ||
| { | ||
| public static readonly TagHelperSet Empty = default!; | ||
|
|
||
| private readonly object _valueOrArray; | ||
|
|
||
| public TagHelperSet(TagHelperDescriptor value) | ||
| { | ||
| _valueOrArray = value; | ||
| } | ||
|
|
||
| public TagHelperSet(TagHelperDescriptor[] array) | ||
| { | ||
| _valueOrArray = array; | ||
| } | ||
|
|
||
| public TagHelperDescriptor this[int index] | ||
| { | ||
| get | ||
| { | ||
| if (_valueOrArray is TagHelperDescriptor[] array) | ||
| { | ||
| return array[index]; | ||
| } | ||
|
|
||
| if (index == 0) | ||
| { | ||
| return (TagHelperDescriptor)_valueOrArray; | ||
| } | ||
|
|
||
| throw new IndexOutOfRangeException(); | ||
| } | ||
| } | ||
|
|
||
| public int Count | ||
| => _valueOrArray switch | ||
| { | ||
| TagHelperDescriptor[] array => array.Length, | ||
| null => 0, | ||
|
|
||
| // _valueOrArray can be an array, a single value, or null. | ||
| // So, we can avoid a type check for the single value case. | ||
| _ => 1 | ||
| }; | ||
|
|
||
| public Enumerator GetEnumerator() | ||
| => new(this); | ||
|
|
||
| public struct Enumerator | ||
| { | ||
| private readonly TagHelperSet _oneOrMany; | ||
| private int _index; | ||
|
|
||
| internal Enumerator(TagHelperSet oneOrMany) | ||
|
ToddGrun marked this conversation as resolved.
Outdated
|
||
| { | ||
| _oneOrMany = oneOrMany; | ||
| _index = -1; | ||
| } | ||
|
|
||
| public bool MoveNext() | ||
| { | ||
| _index++; | ||
| return _index < _oneOrMany.Count; | ||
| } | ||
|
|
||
| public readonly TagHelperDescriptor Current | ||
| => _oneOrMany[_index]; | ||
| } | ||
|
|
||
| private sealed class DebuggerProxy(TagHelperSet instance) | ||
| { | ||
| private readonly TagHelperSet _instance = instance; | ||
|
|
||
| [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] | ||
| public TagHelperDescriptor[] Items | ||
| => _instance._valueOrArray switch | ||
| { | ||
| TagHelperDescriptor[] array => array, | ||
| TagHelperDescriptor value => [value], | ||
| _ => [] | ||
| }; | ||
| } | ||
|
|
||
| private string GetDebuggerDisplay() | ||
| => "Count " + Count; | ||
|
|
||
| /// <summary> | ||
| /// This is a mutable builder for <see cref="TagHelperSet"/>. However, it works differently from | ||
| /// a typical builder. First, you must call <see cref="IncreaseSize"/> to set the number of items. | ||
| /// Once you've done that for each item to be added, you can call <see cref="Add(TagHelperDescriptor)"/> | ||
| /// exactly that many times. This ensures that space allocated is exactly what's needed to | ||
| /// produce the resulting <see cref="TagHelperSet"/>. | ||
| /// </summary> | ||
| public struct Builder | ||
| { | ||
| private object? _valueOrArray; | ||
| private int _index; | ||
| private int _size; | ||
|
|
||
| public void IncreaseSize() | ||
| { | ||
| Debug.Assert(_valueOrArray is null, "Cannot increase size once items have been added."); | ||
| _size++; | ||
| } | ||
|
|
||
| public void Add(TagHelperDescriptor item) | ||
| { | ||
| Debug.Assert(_index < _size, "Cannot add more items."); | ||
|
|
||
| if (_size == 1) | ||
| { | ||
| // We only need to store a single value. | ||
| _valueOrArray = item; | ||
| _index = 1; | ||
| return; | ||
| } | ||
|
|
||
| Debug.Assert(_valueOrArray is null or TagHelperDescriptor[]); | ||
|
|
||
| if (_valueOrArray is not TagHelperDescriptor[] array) | ||
| { | ||
| array = new TagHelperDescriptor[_size]; | ||
| _valueOrArray = array; | ||
| } | ||
|
|
||
| array[_index++] = item; | ||
| } | ||
|
|
||
| public readonly TagHelperSet ToSet() | ||
| { | ||
| Debug.Assert(_index == _size, "Must have added all items."); | ||
|
|
||
| return _size switch | ||
| { | ||
| 0 => Empty, | ||
| 1 => new((TagHelperDescriptor)_valueOrArray!), | ||
| _ => new((TagHelperDescriptor[])_valueOrArray!) | ||
| }; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.