Perf: Use struct for WorkUnitResult#12403
Merged
YuliiaKovalova merged 3 commits intodotnet:mainfrom Oct 9, 2025
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR converts WorkUnitResult from a reference type (class) to a value type (struct) to reduce memory allocations and improve performance. The change addresses excessive allocations occurring during target execution and task execution, particularly when aggregating results across item buckets.
Key changes:
- Convert
WorkUnitResultclass to readonly struct with immutable fields - Update
TargetResultto use a separate lock object instead of locking onWorkUnitResult - Remove null checks and simplify code paths that no longer need to handle null references
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| WorkUnitResult.cs | Convert class to readonly struct, make fields readonly, update serialization logic |
| TargetResult.cs | Add separate lock object and setter for WorkUnitResult property to support struct assignment |
| TaskBuilder.cs | Remove unnecessary null check since structs cannot be null |
| TargetEntry.cs | Update assignment pattern for struct and remove null-conditional operator |
YuliiaKovalova
approved these changes
Oct 9, 2025
This was referenced Feb 23, 2026
Merged
Merged
This was referenced Mar 2, 2026
This was referenced Mar 9, 2026
Merged
Closed
Open
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.
Context
TargetEntry.ExecuteTarget()+TaskBuilder.ExecuteTask()allocate a large number ofWorkUnitResultobjects.Nearly all of these allocations are a result of aggregating results across item buckets, as this produces a new
WorkUnitResultinstance on every single merge. This looks like an easy case for switching to a struct, given these are already treated as immutable and used as value containers (apart from 1 diff here).Overall net ~40MB in this trace (after subtracting the small increase in types seen below +
TargetResult+ extra lock object).Before

After
