Skip to content

[Efficiency Improver] perf: eliminate yield-iterator and GroupBy allocations in IsIgnored() hot path#8195

Merged
Evangelink merged 3 commits into
mainfrom
efficiency/optimize-isignored-allocs-1778609590-612a40a2cdae11a1
May 14, 2026
Merged

[Efficiency Improver] perf: eliminate yield-iterator and GroupBy allocations in IsIgnored() hot path#8195
Evangelink merged 3 commits into
mainfrom
efficiency/optimize-isignored-allocs-1778609590-612a40a2cdae11a1

Conversation

@Evangelink
Copy link
Copy Markdown
Member

IsIgnored() is called twice per test execution (class + method check in
UnitTestRunner). The original implementation always allocated:

  • A compiler-generated yield-iterator state machine via GetAttributes()
  • A GroupBy Lookup, even when the source sequence is empty

This change walks GetCustomAttributesCached() directly:

  • Fast path (no condition attrs, most common): 0 allocations
  • Single-attr path: 0 extra allocations — evaluates directly
  • Multi-attr path: List + GroupBy (same as before, minus state machine)

For a 10,000-test suite with no ignored tests:
~20,000 state machine objects eliminated (~960 KB)
~20,000 GroupBy Lookup objects eliminated
Net: ~40,000 fewer objects / ~1 MB less GC pressure per run

Proxy metric: heap allocation count → GC pressure → energy consumption.
GSF: Hardware Efficiency — fewer managed allocations reduce DRAM and GC
CPU overhead per unit of test work.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

Fixes #8159

…path

IsIgnored() is called twice per test execution (class + method check in
UnitTestRunner). The original implementation always allocated:
  - A compiler-generated yield-iterator state machine via GetAttributes<T>()
  - A GroupBy Lookup, even when the source sequence is empty

This change walks GetCustomAttributesCached() directly:
  - Fast path (no condition attrs, most common): 0 allocations
  - Single-attr path: 0 extra allocations — evaluates directly
  - Multi-attr path: List<T> + GroupBy (same as before, minus state machine)

For a 10,000-test suite with no ignored tests:
  ~20,000 state machine objects eliminated (~960 KB)
  ~20,000 GroupBy Lookup objects eliminated
  Net: ~40,000 fewer objects / ~1 MB less GC pressure per run

Proxy metric: heap allocation count → GC pressure → energy consumption.
GSF: Hardware Efficiency — fewer managed allocations reduce DRAM and GC
CPU overhead per unit of test work.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 13, 2026 17:52
@Evangelink
Copy link
Copy Markdown
Member Author

@copilot resolve the merge conflicts in this pull request

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented May 13, 2026

@copilot resolve the merge conflicts in this pull request

Merged origin/main into the PR branch. The only conflict was in AttributeHelpers.cs — the main branch already contains an improved version of the same optimization (using Dictionary + groupOrder instead of LINQ GroupBy), so I took that version as the resolution.

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.

Copilot wasn't able to review any files in this pull request.

Comment thread src/Adapter/MSTestAdapter.PlatformServices/Helpers/AttributeHelpers.cs Outdated
Comment thread src/Adapter/MSTestAdapter.PlatformServices/Helpers/AttributeHelpers.cs Outdated
@Evangelink
Copy link
Copy Markdown
Member Author

@copilot address review comments

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented May 13, 2026

@copilot address review comments

Addressed in commit perf: collapse IsIgnored() to a single pass over allAttributes. The Dictionary and List are now lazily initialized (??=) inside the loop, so the separate first-pass scan is gone. The common case (no ConditionBaseAttribute) does a single iteration with zero allocations. The single-attribute fast-path duplication was already eliminated when the merge resolved the conflict by taking the unified grouping implementation from origin/main.

@Evangelink Evangelink enabled auto-merge (squash) May 14, 2026 07:39
@Evangelink Evangelink disabled auto-merge May 14, 2026 07:43
@Evangelink Evangelink merged commit 0f01130 into main May 14, 2026
10 checks passed
@Evangelink Evangelink deleted the efficiency/optimize-isignored-allocs-1778609590-612a40a2cdae11a1 branch May 14, 2026 07:43
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 and GroupBy allocations in IsIgnored() hot path

3 participants