Skip to content

[Efficiency Improver] perf: eliminate yield-iterator state machine in PropertyBag.OfType(T)()#7917

Merged
Evangelink merged 3 commits into
mainfrom
efficiency/propertybag-oftype-no-state-machine-8f9c5bd2284aea80
Apr 29, 2026
Merged

[Efficiency Improver] perf: eliminate yield-iterator state machine in PropertyBag.OfType(T)()#7917
Evangelink merged 3 commits into
mainfrom
efficiency/propertybag-oftype-no-state-machine-8f9c5bd2284aea80

Conversation

@Evangelink
Copy link
Copy Markdown
Member

Replace the yield-based iterator from Property.OfType() with a direct linked-list walk inside PropertyBag.OfType().

Before: _property.OfType() (yield return) allocates one state- machine heap object per call, even when no matching property is found.

After: direct while-loop walk; no state-machine allocated; returns [] for the common case (no matching property) with zero heap allocations beyond the empty array constant.

Fixes #7914

Replace the yield-based iterator from Property.OfType<TProperty>() with
a direct linked-list walk inside PropertyBag.OfType<TProperty>().

Before: _property.OfType<TProperty>() (yield return) allocates one state-
machine heap object per call, even when no matching property is found.

After: direct while-loop walk; no state-machine allocated; returns [] for
the common case (no matching property) with zero heap allocations beyond
the empty array constant.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 29, 2026 04:49
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 optimizes a hot-path in Microsoft.Testing.Platform by removing the yield return-based iterator allocation from PropertyBag.OfType<TProperty>() and replacing it with a direct linked-list traversal.

Changes:

  • Replace [.. _property.OfType<TProperty>()] (yield iterator) with a manual while loop over the Property linked list.
  • Preserve existing fast-path behavior for TestNodeStateProperty lookups and the empty/null property-list cases.
Show a summary per file
File Description
src/Platform/Microsoft.Testing.Platform/Messages/PropertyBag.cs Reworks OfType<TProperty>() to avoid iterator state-machine allocations by directly walking the linked list.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 0

Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Summary

Workflow: Test Expert Reviewer 🧪
Date: 2026-04-29
Repository: microsoft/testfx

Key Findings

The PR replaces the yield-based OfType<TProperty>() iterator with a direct linked-list walk, introducing three distinct return paths:

  1. _testNodeStateProperty is TProperty[testNodeStateProperty] (unchanged)
  2. New combined early-return: typeof(TestNodeStateProperty).IsAssignableFrom(typeof(TProperty)) || _property is null[]
  3. New while-loop: !foundAny[], single match → [first!], multiple → [.. overflow]

Paths 2 and 3 (both new) have gaps in PropertyBagTests:

Gap Missing path Risk
_property is null early-return OfType<DummyProperty>() on a bag with only TestNodeStateProperty Silent regression if early-return is dropped
Single-item fast path OfType<DummyProperty>() with exactly 1 match [first!] returns wrong result/throws undetected
No-match path in walk OfType<DummyProperty2>() on a non-empty bag Manual loop doesn't cover LINQ's implicitly empty-on-no-match

The production logic itself looks correct — the overflow initialization overflow ??= [first!] correctly seeds the list with the first match before adding the second. No functional bugs detected.

Recommendations

  1. Add an assertion Assert.IsEmpty(property.OfType<DummyProperty>()) for a bag containing only a TestNodeStateProperty.
  2. Add OfType<DummyProperty>() with exactly 1 DummyProperty in the bag.
  3. Add Assert.IsEmpty(property.OfType<DummyProperty2>()) to the existing OfType_Should_Return_CorrectObject test (one line, costs nothing).

Generated by Test Expert Reviewer

🧪 Test quality reviewed by Test Expert Reviewer 🧪

return !foundAny
? []
: _property is null ? [] : [.. _property.OfType<TProperty>()];
: overflow is not null ? [.. overflow] : [first!];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[Coverage] The single-item fast path ([first!] when overflow is null) is not exercised by any existing test. OfType_Should_Return_CorrectObject only tests with 2 DummyProperty instances, which goes through the overflow branch. The [first!] path needs its own case.

Impact: A regression where a single-result bag returns [] or throws would not be caught.

Suggestion: Extend OfType_Should_Return_CorrectObject or add a dedicated test:

PropertyBag bag = new();
DummyProperty single = new();
bag.Add(single);
bag.Add(PassedTestNodeStateProperty.CachedInstance);
TProperty[] result = bag.OfType<DummyProperty>();
Assert.HasCount(1, result);
Assert.AreSame(single, result[0]);


// We don't want to allocate an array if we know that we're looking for a TestNodeStateProperty
return typeof(TestNodeStateProperty).IsAssignableFrom(typeof(TProperty))
if (typeof(TestNodeStateProperty).IsAssignableFrom(typeof(TProperty)) || _property is null)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[Coverage] The || _property is null branch is new (the original ternary had _property is null ? [] as its own arm). There's currently no test for calling OfType<SomeDummyProperty>() on a PropertyBag that contains only a TestNodeStateProperty (i.e., _testNodeStateProperty is set but _property is null).

Impact: The early-return path added here goes unverified. If a future refactor accidentally removes it, a silent linear walk over a null reference would produce incorrect results.

Suggestion: Add a test case to PropertyBagTests:

[TestMethod]
public void OfType_WithOnlyTestNodeStateProperty_ReturnsEmpty()
{
    PropertyBag property = new();
    property.Add(PassedTestNodeStateProperty.CachedInstance);
    // _property is null; _testNodeStateProperty is set — exercises the new early-return path
    Assert.IsEmpty(property.OfType<DummyProperty>());
}

current = current.Next;
}

return !foundAny
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[Coverage] The !foundAny ? [] return at the end of the while-loop (no matching property found in a non-empty bag) is not tested. DummyProperty2 is used in Single / SingleOrDefault tests, but OfType<DummyProperty2>() is never called on a bag that has other properties.

Impact: The new manual traversal diverges from the old LINQ-based _property.OfType<TProperty>(). A bug that incorrectly returns null or throws instead of [] when no match is found would go undetected.

Suggestion: Add one assertion to the existing OfType_Should_Return_CorrectObject test:

// No DummyProperty2 in the bag — exercises the "no match found" path in the while-loop
Assert.IsEmpty(property.OfType<DummyProperty2>());

@Evangelink
Copy link
Copy Markdown
Member Author

@copilot address review comments

Copilot AI review requested due to automatic review settings April 29, 2026 05:16
@Evangelink Evangelink review requested due to automatic review settings April 29, 2026 05:16
Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 29, 2026

@copilot address review comments

Added the three missing test cases in commits 911b697 and 813c705:

  • OfType_Should_Return_CorrectObject — extended with Assert.IsEmpty(property.OfType<DummyProperty2>()) to cover the "no match found" path
  • OfType_WithSingleMatch_ReturnsSingleItemArray — new test covering the single-item fast path ([first!] when overflow is null)
  • OfType_WithOnlyTestNodeStateProperty_ReturnsEmpty — new test covering the _property is null early return when only a TestNodeStateProperty is present

@Evangelink Evangelink disabled auto-merge April 29, 2026 09:14
@Evangelink Evangelink merged commit e309705 into main Apr 29, 2026
24 checks passed
@Evangelink Evangelink deleted the efficiency/propertybag-oftype-no-state-machine-8f9c5bd2284aea80 branch April 29, 2026 09:14
Evangelink added a commit that referenced this pull request May 12, 2026
… PropertyBag.OfType(T)() (#7917)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+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.

[Efficiency Improver] perf: eliminate yield-iterator state machine in PropertyBag.OfType(T)()

3 participants