Skip to content

Perf: Use struct for WorkUnitResult#12403

Merged
YuliiaKovalova merged 3 commits intodotnet:mainfrom
ccastanedaucf:dev/chcasta/perf-workunitresult
Oct 9, 2025
Merged

Perf: Use struct for WorkUnitResult#12403
YuliiaKovalova merged 3 commits intodotnet:mainfrom
ccastanedaucf:dev/chcasta/perf-workunitresult

Conversation

@ccastanedaucf
Copy link
Contributor

Context

TargetEntry.ExecuteTarget() + TaskBuilder.ExecuteTask() allocate a large number of WorkUnitResult objects.

Nearly all of these allocations are a result of aggregating results across item buckets, as this produces a new WorkUnitResult instance 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
image

After
image

Copilot AI review requested due to automatic review settings August 23, 2025 00:41
Copy link
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 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 WorkUnitResult class to readonly struct with immutable fields
  • Update TargetResult to use a separate lock object instead of locking on WorkUnitResult
  • 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 YuliiaKovalova self-assigned this Oct 6, 2025
@YuliiaKovalova YuliiaKovalova merged commit e0ef001 into dotnet:main Oct 9, 2025
9 checks passed
This was referenced Mar 9, 2026
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