avoid some redundant enumerable alloc#4972
avoid some redundant enumerable alloc#4972SimonCropp wants to merge 3 commits intothomhurst:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This is a clean, well-targeted optimization. Eliminating the async/yield return wrappers that were purely forwarding elements is exactly the right call.
Why this is correct
Each async IAsyncEnumerable method backed by a yield return causes the C# compiler to generate a state machine class. When the wrapper does nothing but forward items, you're allocating that state machine per call for zero benefit. Returning the inner IAsyncEnumerable<T> directly is semantically equivalent here and avoids the allocation entirely — consistent with the project's "Performance First" mandate.
There's one subtle behavioral difference worth being aware of: in the old wrapper form, any synchronous exception thrown inside GenerateDataSourcesAsync before its first MoveNextAsync would be deferred to the enumeration phase. With the direct return, such an exception propagates immediately to the caller. In practice this is a non-issue here — GenerateDataSourcesAsync is itself an async iterator in derived classes and won't throw synchronously before returning the IAsyncEnumerable — but it's worth noting for future changes.
AOT benefit
Fewer compiler-generated state machine types also reduces the AOT codegen surface, which aligns well with TUnit's Native AOT compatibility requirement.
Minor observation (out of scope for this PR)
After this change in AsyncUntypedDataSourceSourceGeneratorAttribute, the call chain is:
GetDataRowsAsync → GenerateAsync → GenerateDataSourcesAsync
GenerateAsync is now a pure pass-through. GetDataRowsAsync could call GenerateDataSourcesAsync directly, but that's a separate refactor and the current structure is fine as-is.
The removed comment in AsyncUntypedDataSourceSourceGeneratorAttribute.cs was only documenting intent in the now-deleted loop body, so removing it is appropriate.
Overall: LGTM. Straightforward, correct, and beneficial to performance and AOT compatibility.
No description provided.