dotnet/roslyn

Generalize `(CollectionBuilderType<T>)[..readOnlySpan]` optimization to spread operands which are implicitly convertible to `ReadOnlySpan<T>`

Open

#73,110 opened on Apr 19, 2024

View on GitHub
 (5 comments) (0 reactions) (0 assignees)C# (4,257 forks)batch import
Area-CompilersCode Gen QualityFeature - Collection Expressionshelp wanted

Repository metrics

Stars
 (20,414 stars)
PR merge metrics
 (Avg merge 6d 17h) (256 merged PRs in 30d)

Description

I would welcome a follow-up PR which also handles cases where the spread operand is implicitly convertible to ReadOnlySpan<T>.

Originally posted by @RikkiGibson in https://github.com/dotnet/roslyn/issues/73102#issuecomment-2067272169

Affected scenarios would include:

Span<int> span = ..;
MyCollectionBuilderType<T> collection = [..span];

// currently:
MyCollectionBuilderType<T> collection = MyCollectionBuilderType.Create((ReadOnlySpan<T>)span.ToArray());

// could change to:
MyCollectionBuilderType<T> collection = MyCollectionBuilderType.Create((ReadOnlySpan<T>)span);


Contributor guide