Extend ToArray optimization when targeting read-only collection types#80104
Extend ToArray optimization when targeting read-only collection types#80104RikkiGibson merged 32 commits intodotnet:mainfrom
ToArray optimization when targeting read-only collection types#80104Conversation
…eference-converted to the target array
|
Can I have a review here? @RikkiGibson ? |
src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_CollectionExpression.cs
Outdated
Show resolved
Hide resolved
|
I am concerned that the result of ToArray(), which we are doing a covariant conversion on, will be ultimately converted to Span and throw at runtime. See the following test which throws during execution in this branch [Fact]
public void SingleSpread_SpanToSpan_Covariant()
{
var source = """
using System;
class C
{
static void Main()
{
Span<string> span1 = ["a", "b", "c"];
span1.Report();
Span<object> span2 = [..span1];
span2.Report();
}
}
""";
var verifier = CompileAndVerify(new[] { source, s_collectionExtensionsWithSpan }, expectedOutput: IncludeExpectedOutput("[a, b, c], [a, b, c],"), targetFramework: TargetFramework.Net80, verify: Verification.Skipped);
verifier.VerifyDiagnostics();
verifier.VerifyIL("C.Main", """
TODO2: update baseline
""");
}I think this optimization needs to be limited to cases where the collection-expr type is a "readonly" type, such as We can't use this optimization when the collection-expr is of an array type, for similar reasons as the Span case above. Today the array's runtime type is the same as the element type. If we start returning a Could you please also add a test which does something like |
|
Taking another look this afternoon. |
src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_CollectionExpression.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_CollectionExpression.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_CollectionExpression.cs
Show resolved
Hide resolved
src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionTests.cs
Outdated
Show resolved
Hide resolved
RikkiGibson
left a comment
There was a problem hiding this comment.
Looking much better and close to being able to sign off. Thanks
src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_CollectionExpression.cs
Outdated
Show resolved
Hide resolved
|
I pushed 9d61d77 to refactor VisitArrayOrSpanCollectionExpression. This is a pure refactor as much as possible. I went into the original code, wrapped some sections in local functions, and moved some things around from one function to another, in order to make each path more focused on a specific task. I also tried to eliminate locals when they did not provide significant value, in order to make the set of data flowing from one function to another easier to follow. |
|
@jcouv for a new review pass |
| @@ -5046,6 +5045,1016 @@ static void Main() | |||
| Assert.Equal(CollectionExpressionTypeKind.ImplementsIEnumerable, ConversionsBase.GetCollectionExpressionTypeKind(comp, collectionType, out _)); | |||
| } | |||
|
|
|||
| [Theory, CombinatorialData] | |||
There was a problem hiding this comment.
to prevent massive collisions. Can you put the new teests in a new file. That will make my life much niccer.
src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_CollectionExpression.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_CollectionExpression.cs
Outdated
Show resolved
Hide resolved
AlekseyTs
left a comment
There was a problem hiding this comment.
Quickly glanced over compiler changes (commit 31). See other comments for details.
|
@CyrusNajmabadi I tried merging the PR branch for #80568 into this PR branch locally. I found the conflicts fairly straightforward to handle, including that there were no conflicts in the tests. See the conflict resolutions in RikkiGibson@1d3ce15. |
The comments were addressed in commit 32
|
@RikkiGibson Can we merge now (assuming CI passes after rerun)? |
|
It looks like the failing test jobs were "dead letter" work items in Helix. I suspect this is due to some infrastructure issue. Will plan to merge if the rerun passes. Thanks |
|
Thanks for the contribution @DoctorKrolic! |
ToArryoptimization for covariant conversions when targetingReadOnlySpan,ImmutableArray,IEnumerable,IReadOnlyCollectionorIReadOnlyListToArrayoptimization when types are different in C# but identical in runtime (e.g.objectvsdynamic)CopyTooptimization in the same scenario