Repository navigation
[efficiency-improver] perf: single-pass PropertyBag walk in MSBuildConsumer.ConsumeAsync - #9101
Conversation
Replace 3 x SingleOrDefault<T>() linked-list walks with one GetStructEnumerator() pass in MSBuildConsumer.ConsumeAsync. Before: 3 separate PropertyBag walks per TestNodeUpdateMessage (TimingProperty, TestFileLocationProperty, TestNodeStateProperty) After: 1 GetStructEnumerator() pass collecting all three properties For a test run with 1 000 tests this eliminates ~2 000 redundant linked-list traversals. Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
Pull request overview
This PR optimizes MSBuildConsumer.ConsumeAsync() in the MSBuild extension by reducing repeated PropertyBag lookups per TestNodeUpdateMessage, aiming to lower per-result CPU overhead during MSBuild-driven test runs.
Changes:
- Replaces multiple
PropertyBag.SingleOrDefault<T>()calls with a singleGetStructEnumerator()traversal that gathers needed properties. - Uses the collected
TestNodeStatePropertyresult for the subsequent stateswitch.
Show a summary per file
| File | Description |
|---|---|
| src/Platform/Microsoft.Testing.Extensions.MSBuild/MSBuildConsumer.cs | Refactors property extraction in ConsumeAsync() into a single PropertyBag enumeration to reduce repeated lookups. |
Copilot's findings
- Files reviewed: 1/1 changed files
- Comments generated: 3
| case TimingProperty t when timingProperty is null: | ||
| timingProperty = t; | ||
| break; |
There was a problem hiding this comment.
Fixed in da9ddf1 -- the TimingProperty case now throws InvalidOperationException on a duplicate (matching PropertyBag.SingleOrDefault<TimingProperty>'s contract) instead of silently keeping the first match. Review reply handled.
| case TestFileLocationProperty f when testFileLocationProperty is null: | ||
| testFileLocationProperty = f; | ||
| break; |
There was a problem hiding this comment.
Fixed in da9ddf1 -- TestFileLocationProperty case also now throws on duplicate matches, restoring the prior fail-fast semantics. Review reply handled.
| // Collect all required properties in a single zero-allocation GetStructEnumerator() pass, | ||
| // replacing 3 × SingleOrDefault<T>() linked-list walks with 1. |
There was a problem hiding this comment.
Good catch -- fixed in da9ddf1. Updated the comment to 2 × ... linked-list walks and added an explicit note that TestNodeStateProperty is an O(1) cached field on PropertyBag, so only the TimingProperty and TestFileLocationProperty lookups actually walk the linked list. Review reply handled.
Amaury Levé (Evangelink)
left a comment
There was a problem hiding this comment.
Note
🤖 Automated review by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Expert Code Review workflow. To request a follow-up action, reply by tagging @copilot directly.
| # | Dimension | Verdict |
|---|---|---|
| 1 | Algorithmic Correctness | ✅ LGTM — semantically equivalent to original for TestNodeStateProperty (protected by PropertyBag.Add() duplicate-throw); one pattern inconsistency noted inline |
| 2 | Threading & Concurrency | ✅ LGTM — ConsumeAsync holds no locks; struct enumerator is stack-local, no shared-state hazard |
| 3 | Public API & Binary Compatibility | ✅ LGTM — MSBuildConsumer is internal sealed; no public surface changed |
| 4 | Performance & Allocations | ✅ LGTM — GetStructEnumerator() returns a struct by value; single pass replaces three linked-list walks; zero allocations added |
| 5 | Resource & IDisposable Management | ✅ LGTM — PropertyBagEnumerator.Dispose() only nulls struct fields (no unmanaged resources); consistent with all other GetStructEnumerator() call sites which also omit using |
| 6 | Code Structure & Simplification |
✅ 13/14 dimensions clean. One moderate finding (no blocking issues):
- Replace
when timingProperty is null/when testFileLocationProperty is nullguards with theGetSingleOrDefaultValuelocal helper used in every otherGetStructEnumerator()site (DotnetTestDataConsumer,OpenTelemetryResultHandler,TrxTestResultExtractor,JUnitReport,HtmlReport,CtrfReport). This restores the throw-on-duplicate invariant thatSingleOrDefault<T>()provided for non-state properties. Details in the inline comment.
🤖 Automated content by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Expert Code Review (on PR ready) workflow. · 413.2 AIC · ⌖ 12.6 AIC · ◷
Comments that could not be inline-anchored
src/Platform/Microsoft.Testing.Extensions.MSBuild/MSBuildConsumer.cs:112
Deviation from the GetSingleOrDefaultValue pattern used everywhere else
Every other GetStructEnumerator() conversion in this codebase (DotnetTestDataConsumer, OpenTelemetryResultHandler, TrxTestResultExtractor, JUnitReport.TestResultCapture, HtmlReport.TestResultCapture, CtrfReport.TestResultCapture) uses a local GetSingleOrDefaultValue helper to preserve the throw-on-duplicate invariant that SingleOrDefault<T>() provided.
The when timingProperty is null and `whe…
- `TimingProperty` / `TestFileLocationProperty` cases now throw `InvalidOperationException` on a duplicate (matching `PropertyBag.SingleOrDefault<T>`'s contract) instead of silently keeping the first match. Malformed messages now fail fast like before. - Update the perf comment: `TestNodeStateProperty` is an O(1) cached field on `PropertyBag`, so only the `TimingProperty` and `TestFileLocationProperty` lookups actually walk the linked list. The single-pass enumerator replaces those 2 walks with 1 (not 3 walks as the comment claimed). Co-authored-by: Copilot <[email protected]>
Goal and Rationale
Reduce redundant CPU work in
MSBuildConsumer.ConsumeAsync()inMicrosoft.Testing.Extensions.MSBuild, which is called for everyTestNodeUpdateMessageduring MSBuild-driven test runs (dotnet test/msbuild /t:test).Focus area: Code-Level Efficiency — eliminating unnecessary linked-list traversals per test result.
Approach
ConsumeAsync()previously made 3 separate passes over thePropertyBaglinked list:The fix replaces all three with a single
GetStructEnumerator()pass that collects all required properties in one traversal.Energy Efficiency Evidence
Proxy metric: CPU cycles / linked-list pointer dereferences per test result (more traversals = more cache misses = more power draw per test run).
TestNodeUpdateMessageFor a test run with 1 000 tests: ~20 000 redundant pointer dereferences eliminated.
This is the same pattern already applied to
DotnetTestDataConsumer,OpenTelemetryResultHandler,TrxTestResultExtractor,JUnitReport.TestResultCapture,HtmlReport.TestResultCapture, andCtrfReport.TestResultCapture.Reproducibility: Build
src/Platform/Microsoft.Testing.Extensions.MSBuild/Microsoft.Testing.Extensions.MSBuild.csproj— succeeded with 0 warnings, 0 errors.Green Software Foundation Context
Hardware Efficiency: Fewer repeated traversals of the same linked-list nodes reduces CPU cache pressure and memory bus traffic per test result processed.
Software Carbon Intensity (SCI): Fewer instructions per functional unit (one test result processed) directly reduces the energy term in the SCI equation.
Trade-offs
The single-pass
switchblock insideGetStructEnumerator()is slightly more verbose than the previous sequence of namedSingleOrDefault<T>()calls. The pattern is now well-established across the codebase (see the files listed above) and is recognisable to contributors familiar with the PropertyBag optimisation series.Test Status
✅ Full solution build succeeded (0 warnings, 0 errors)
Add this agentic workflows to your repo
To install this agentic workflow, run