Remove some redundant immutable array allocation#4988
Conversation
There was a problem hiding this comment.
Code Review
Summary: This PR removes redundant allocations across 4 files. All changes are correct and beneficial.
TUnit.Analyzers/TestDataAnalyzer.cs
Removing is valid — already returns (per ), so calling on it was a no-op allocation. Good catch.
TUnit.Assertions.SourceGenerator/Generators/AssertionMethodGenerator.cs
Removing before is correct. Roslyn's accepts directly — the intermediate conversion to was purely wasteful.
TUnit.Core.SourceGenerator/Generators/PropertyInjectionSourceGenerator.cs
Three cleanups bundled here:
-
Return type change ( → ): Valid because immediately unpacks the container, so it's never stored as intermediate state in the generator pipeline. The caching concern for incremental generators applies to the items (individual values), not the container wrapper. Worth noting: if implements , downstream caching works correctly regardless of container type.
-
Early-exit collection expression ( instead of ): Cleaner.
-
** → **: Correct update for .
One minor observation: the variable is now declared after the early-return guard, which actually improves readability — no allocation happens on the hot path when or is null.
TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs
Removing before is correct — accepts (line 13 of ). The concat chain of is now lazily consumed rather than materialized first.
Overall: Clean, targeted micro-optimizations that reduce unnecessary heap allocations in both analyzer and source generator hot paths. All changes are correctness-safe. No issues found.
There was a problem hiding this comment.
Code Review
Summary: This PR removes redundant ImmutableArray allocations across 4 files. All changes are correct and beneficial.
TUnit.Analyzers/TestDataAnalyzer.cs
Removing .ToImmutableArray() is valid. WithoutCancellationTokenParameter() already returns ImmutableArray<IParameterSymbol> (per ParameterExtensions.cs:9), so calling .ToImmutableArray() on it was a no-op allocation. Good catch.
TUnit.Assertions.SourceGenerator/Generators/AssertionMethodGenerator.cs
Removing .ToImmutableArray() before SelectMany is correct. Roslyn's IncrementalValuesProvider.SelectMany accepts IEnumerable<T> directly, so the intermediate conversion to ImmutableArray was purely wasteful.
TUnit.Core.SourceGenerator/Generators/PropertyInjectionSourceGenerator.cs
Three cleanups bundled here:
-
Return type change from ImmutableArray to List: Valid because
SelectManyimmediately unpacks the container, so it is never stored as intermediate pipeline state. The incremental generator caching concern applies to the items themselves (individualConcreteGenericTypeModelvalues), not the container wrapper. IfConcreteGenericTypeModelimplementsIEquatable<T>, downstream caching works correctly regardless of container type. -
Early-exit collection expression (
return []instead ofreturn ImmutableArray<ConcreteGenericTypeModel>.Empty): Cleaner modern C# syntax. -
x.Lengthtox.Count: Correct update forList<T>.
Minor positive side effect: moving results declaration after the early-return guard means no allocation happens on the fast path when dataSourceInterface or asyncInitializerInterface is null.
TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs
Removing .ToImmutableArray() before WriteAttributes() is correct. AttributeWriter.WriteAttributes accepts IEnumerable<AttributeData> (line 13 of AttributeWriter.cs), so the concat chain is now lazily consumed rather than eagerly materialized first.
Overall: Clean, targeted micro-optimizations that reduce unnecessary heap allocations in both analyzer and source generator hot paths. All changes are correctness-safe. No issues found.
No description provided.