Skip to content

Users/tevinstanley/allocatelistsizecorrectly#12027

Merged
SimaTian merged 4 commits intodotnet:mainfrom
ttstanley:users/tevinstanley/allocatelistsizecorrectly
Feb 13, 2026
Merged

Users/tevinstanley/allocatelistsizecorrectly#12027
SimaTian merged 4 commits intodotnet:mainfrom
ttstanley:users/tevinstanley/allocatelistsizecorrectly

Conversation

@ttstanley
Copy link
Copy Markdown
Contributor

@ttstanley ttstanley commented Jun 17, 2025

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.
image

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.
image

Notes

@ttstanley ttstanley marked this pull request as ready for review June 24, 2025 17:54
Copilot AI review requested due to automatic review settings June 24, 2025 17:54
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 parameterValues list across loop iterations, clearing it instead of instantiating a new list each time.
  • In GetBatchableValuesFromBuildItemGroupChild, adjust parameterValues.Capacity based on child.Metadata.Count + 4 to avoid repeated resizing.
Comments suppressed due to low confidence (1)

src/Build/BackEnd/Components/RequestBuilder/IntrinsicTasks/ItemGroupIntrinsicTask.cs:143

  • Clearing parameterValues outside of a finally block may lead to stale data in case of an exception or a NullReferenceException if the list was never initialized. Consider moving this into a finally or guarding it with a null check.
                    parameterValues.Clear();

…mGroupIntrinsicTask.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@SimaTian SimaTian merged commit 5a78f0f into dotnet:main Feb 13, 2026
9 checks passed
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.

![image](https://github.com/user-attachments/assets/5ee67e97-09d0-4d87-9605-8f7ed11955e3)


### 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.

![image](https://github.com/user-attachments/assets/76f6f8af-a680-47ff-a6b7-6464368ee574)



### 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.

![image](https://github.com/user-attachments/assets/5ee67e97-09d0-4d87-9605-8f7ed11955e3)


### 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.

![image](https://github.com/user-attachments/assets/76f6f8af-a680-47ff-a6b7-6464368ee574)



### Notes

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants