Skip to content
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

Poor codegen for collection expression spread (ROS<T> -> ImmutableArray<T>) #71292

Closed
Sergio0694 opened this issue Dec 16, 2023 · 1 comment
Closed
Labels
Area-Compilers Code Gen Quality Room for improvement in the quality of the compiler's generated code Feature - Collection Expressions untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@Sergio0694
Copy link
Contributor

Sergio0694 commented Dec 16, 2023

Related to #71195

Version Used: 4.9.0-ci (a8119d1)

Steps to Reproduce:

using System;
using System.Collections.Immutable;

static unsafe ImmutableArray<byte> Foo(byte* p, int length)
{
    return [.. new ReadOnlySpan<byte>(p, length)];
}

sharplab

Expected Behavior:

ReadOnlySpan<byte> readOnlySpan = new ReadOnlySpan<byte>(p, length);
return ImmutableArray.CreateRange(readOnlySpan);

OR

ReadOnlySpan<byte> readOnlySpan = new ReadOnlySpan<byte>(p, length);
return ImmutableCollectionsMarshal.AsImmutableArray(readOnlySpan.ToArray());

Actual Behavior:

ReadOnlySpan<byte> readOnlySpan = new ReadOnlySpan<byte>(p, length);
int num = 0;
byte[] array = new byte[readOnlySpan.Length];
ReadOnlySpan<byte>.Enumerator enumerator = readOnlySpan.GetEnumerator();
while (enumerator.MoveNext())
{
    byte b = (array[num] = enumerator.Current);
    num++;
}
return ImmutableCollectionsMarshal.AsImmutableArray(array);

cc. @RikkiGibson maybe a missed case from your other optimization work? 🤔

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 16, 2023
@jcouv jcouv added Feature - Collection Expressions Code Gen Quality Room for improvement in the quality of the compiler's generated code labels Dec 18, 2023
@RikkiGibson
Copy link
Contributor

I think sharplab may not have had the spread optimization present at the time this issue was filed. Seeing the following codegen now SharpLab

return ImmutableCollectionsMarshal.AsImmutableArray(new ReadOnlySpan<byte>(p, length).ToArray());

I'll close as no repro. Feel free to follow up here if I missed something. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Code Gen Quality Room for improvement in the quality of the compiler's generated code Feature - Collection Expressions untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

3 participants