-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Avoid boxing and unnecessary array allocations #12175
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
Avoid boxing and unnecessary array allocations #12175
Conversation
There was a problem hiding this 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 optimizes performance by reducing boxing allocations and unnecessary array allocations in the BuildRequestEngine. The main issue addressed is the automatic boxing of BuildRequestEngineStatus enum values when passed to ErrorUtilities.VerifyThrow() calls that accept object parameters.
- Introduces a
BuildRequestEngineStatusBoxesclass with pre-boxed enum values and an extension method to avoid repeated boxing - Updates
ErrorUtilities.VerifyThrow()calls to use the new boxing extension method - Adds overloaded
TraceEngine()methods to prevent array allocations in debugging scenarios
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| IBuildRequestEngine.cs | Adds BuildRequestEngineStatusBoxes class with cached boxed enum values and Box() extension method |
| BuildRequestEngine.cs | Updates enum boxing calls and adds overloaded TraceEngine methods to reduce allocations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future, I'd prefer to see these changes separated out; I found it confusing to see the unrelated changes in the same PR.
src/Build/BackEnd/Components/BuildRequestEngine/BuildRequestEngine.cs
Outdated
Show resolved
Hide resolved
Curious where/how you'd split this. The paths seemed related to me, so it seemed natural to group them. |
|
I'd have done
then each is tiny, scoped, and trivial to review. Here I got confused about |
Will do! Thanks for the input |
YuliiaKovalova
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, Eric!
|
@Erarndt please address the remaining comments and we are ready to go |
@Erarndt do you plan to make changes regarding this comment? |
Unless I misunderstood, @rainersigwald requested that I split out the separate changes in the future. No changes requested for this PR. |
|
Correct. |
There are several delegates called during build that cause some sneaky allocations. For instance, there is boxing of the `BuildRequestEngineStatus` enum. The culprit is the `ErrorUtilities.VerifyThrow()` calls that take `object` as parameters for the formatted message. To make the call, the enum gets boxed even when the condition check returns `true`. Adding additional overloads that take the specific type lets multiple callers benefit, and I did some additional cleanup in the delegates to remove some allocations.
Fixes #
Context
There are several delegates called during build that cause some sneaky allocations. For instance, look at the boxing of the
BuildRequestEngineStatusenum:The culprit is the
ErrorUtilities.VerifyThrow()calls that takeobjectas parameters for the formatted message. To make the call, the enum gets boxed even when the condition check returnstrue. Adding additional overloads that take the specific type lets multiple callers benefit, and I did some additional cleanup in the delegates to remove some allocations.Before:
After:
The


UnblockBuildRequestdelegate has no allocationsChanges Made
Testing
Notes