Users/tevinstanley/allocatelistsizecorrectly#12027
Merged
SimaTian merged 4 commits intodotnet:mainfrom Feb 13, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR reduces memory allocations by reusing and pre-sizing a single list of strings instead of recreating and dynamically resizing it on each iteration.
- Reuse the same
parameterValueslist across loop iterations, clearing it instead of instantiating a new list each time. - In
GetBatchableValuesFromBuildItemGroupChild, adjustparameterValues.Capacitybased onchild.Metadata.Count + 4to avoid repeated resizing.
Comments suppressed due to low confidence (1)
src/Build/BackEnd/Components/RequestBuilder/IntrinsicTasks/ItemGroupIntrinsicTask.cs:143
- Clearing
parameterValuesoutside of afinallyblock may lead to stale data in case of an exception or a NullReferenceException if the list was never initialized. Consider moving this into afinallyor guarding it with a null check.
parameterValues.Clear();
src/Build/BackEnd/Components/RequestBuilder/IntrinsicTasks/ItemGroupIntrinsicTask.cs
Outdated
Show resolved
Hide resolved
…mGroupIntrinsicTask.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
SimaTian
approved these changes
Feb 13, 2026
Copilot AI
pushed a commit
that referenced
this pull request
Feb 17, 2026
Fixes # ### Context On traces we could see about 8 mb of allocations due to add nonempty strings. When taking it look at the code we could see alot of adding to a list which was constantly resizing.  ### Changes Made * Instead of defaulting a list of default size we use context to initialize the size to be something closer to what we would like. So that the capacity only needs to be set once throughout its use. * Also reuse the same list throughout instead of using a new one every time. ### Testing After the changes I took a trace and it shows that this path is has been reduced by 5mb.  ### Notes --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
JanProvaznik
pushed a commit
to JanProvaznik/msbuild
that referenced
this pull request
Feb 25, 2026
Fixes # ### Context On traces we could see about 8 mb of allocations due to add nonempty strings. When taking it look at the code we could see alot of adding to a list which was constantly resizing.  ### Changes Made * Instead of defaulting a list of default size we use context to initialize the size to be something closer to what we would like. So that the capacity only needs to be set once throughout its use. * Also reuse the same list throughout instead of using a new one every time. ### Testing After the changes I took a trace and it shows that this path is has been reduced by 5mb.  ### Notes --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.
Fixes #
Context
On traces we could see about 8 mb of allocations due to add nonempty strings. When taking it look at the code we could see alot of adding to a list which was constantly resizing.

Changes Made
Testing
After the changes I took a trace and it shows that this path is has been reduced by 5mb.

Notes