Reduce allocations in PENamedTypeSymbol.GetMemberTypesPrivate#81605
Merged
CyrusNajmabadi merged 2 commits intodotnet:mainfrom Dec 11, 2025
Merged
Conversation
Comment on lines
+1798
to
+1804
| int capacity = 0; | ||
| foreach (var typeArray in _lazyNestedTypes.Values) | ||
| { | ||
| capacity += typeArray.Length; | ||
| } | ||
|
|
||
| var builder = ArrayBuilder<NamedTypeSymbol>.GetInstance(capacity); |
Contributor
There was a problem hiding this comment.
Suggested change
| int capacity = 0; | |
| foreach (var typeArray in _lazyNestedTypes.Values) | |
| { | |
| capacity += typeArray.Length; | |
| } | |
| var builder = ArrayBuilder<NamedTypeSymbol>.GetInstance(capacity); | |
| var builder = ArrayBuilder<NamedTypeSymbol>.GetInstance(_lazyNestedTypes.Values.Sum(static a => a.Length)); |
Contributor
There was a problem hiding this comment.
If we are doing the work to determine the exact capacity up front, then this would be better off just creating the array explicitly, rather than using an ArrayBuilder and hitting the pool, something like:
private ImmutableArray<NamedTypeSymbol> GetMemberTypesPrivate()
{
var count = _lazyNestedTypes.Values.Sum(static a => a.Length);
var result = new PENamedTypeSymbol[count];
var destIndex = 0;
foreach (var typeArray in _lazyNestedTypes.Values)
{
typeArray.CopyTo(result, destIndex);
destIndex += typeArray.Length;
}
return ImmutableCollectionsMarshal.AsImmutableArray<NamedTypeSymbol>(result);
}
Contributor
Author
There was a problem hiding this comment.
Updated to use explicit array.
Contributor
Could you link me to those ETW traces. You can read me internally if needed. |
333fred
approved these changes
Dec 10, 2025
CyrusNajmabadi
approved these changes
Dec 11, 2025
Contributor
|
Thanks! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This pull request was generated by the VS Perf Rel AI Agent. Please review this AI-generated PR with extra care! For more information, visit our wiki. Please share feedback with TIP Insights
Best practices wiki
See related failure in PRISM
ADO work item