Skip to content

use struct enumerator instead of using IEnumerable through yield retu… #11984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

ttstanley
Copy link
Contributor

@ttstanley ttstanley commented Jun 9, 2025

…rns to prevent allocations

Fixes #

Context

We can see through traces that there are allocations due to the statemachine created to track yield returning IEnumerable.
(This can be shown with the type d_29 in image below.)
image

Changes Made

  • To fix this issue, I created a struct enumerator that would wrap the collection to enumerate over rather than needing to use IEnumerable through yield returns.

Notes

@ttstanley ttstanley marked this pull request as ready for review June 12, 2025 20:50
@Copilot Copilot AI review requested due to automatic review settings June 12, 2025 20:50
Copy link
Contributor

@Copilot 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 replaces the IEnumerable<AssemblyEntry>-based iterator in FindAssemblyNameFromSimpleName with a custom struct enumerator to eliminate heap allocations from the compiler-generated state machine.

  • Return a new AssemblyNameFromSimpleNameEnumerator struct instead of using yield return
  • Introduce AssemblyNameFromSimpleNameEnumerator in RedistList.cs
  • Update call sites (including unit tests) to consume the struct-based enumerator

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/Tasks/SystemState.cs Inlined the call to FindAssemblyNameFromSimpleName in the loop
src/Tasks/RedistList.cs Changed return type to struct enumerator and implemented it
src/Tasks/AssemblyDependency/InstalledAssemblies.cs Adapted signature and return of FindAssemblyNameFromSimpleName
src/Tasks.UnitTests/AssemblyDependency/Miscellaneous.cs Updated test to use C# 12 collection expression
Comments suppressed due to low confidence (2)

src/Tasks/RedistList.cs:486

  • Changing the return type from IEnumerable<AssemblyEntry> to a struct enumerator removes the IEnumerable<T> interface, which may break existing callers (e.g., LINQ or .ToList() usage). Consider implementing IEnumerable<AssemblyEntry> on the struct or providing an explicit AsEnumerable() method for compatibility.
internal AssemblyNameFromSimpleNameEnumerator FindAssemblyNameFromSimpleName(string simpleName)

src/Tasks/RedistList.cs:491

  • [nitpick] The new AssemblyNameFromSimpleNameEnumerator struct lacks XML doc comments. Adding summaries for the struct, its constructors, MoveNext(), and GetEnumerator() will improve maintainability and clarity.
internal struct AssemblyNameFromSimpleNameEnumerator

Copy link
Member

@JanProvaznik JanProvaznik left a comment

Choose a reason for hiding this comment

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

It's sad when nicer code means worse performance. I suppose this task is so frequent it's technically not premature optimization.

Co-authored-by: Rainer Sigwald <raines@microsoft.com>
@YuliiaKovalova YuliiaKovalova merged commit e0f2c8d into dotnet:main Jul 7, 2025
9 checks passed
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.

4 participants