dotnet/roslyn

Collection expressions can perform covariant conversion of ReadOnlySpan in emit

Open

#71.106 geöffnet am 5. Dez. 2023

Auf GitHub ansehen
 (6 Kommentare) (1 Reaktion) (1 zugewiesene Person)C# (4.257 Forks)batch import
Area-CompilersCode Gen QualityFeature - Collection Expressionshelp wanted

Repository-Metriken

Stars
 (20.414 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (256 gemergte PRs in 30 T)

Beschreibung

The runtime doesn't provide a safe facility for covariant conversion of ReadOnlySpan, but it is supported to perform such conversions. If the compiler performed such conversions, it would enable more optimizations in scenarios like the following:

Span<Derived> span1 = ..., span2 = ...;
ReadOnlySpan<Base> spanBase1 = [..span1]; // emit as conversion to ReadOnlySpan<Base> then 'ToArray()'
ReadOnlySpan<Base> spanBase2 = [..span1, ..span2]; // emit as conversion then 'CopyTo()' the backing collection of 'spanBase2'

To start with, we are only going to perform ToArray and CopyTo optimizations in the case where the spread operator and the target collection have the same element type.

Contributor Guide