dotnet/roslyn

Collection expressions can perform covariant conversion of ReadOnlySpan in emit

开放

#71,106 创建于 2023年12月5日

 (6 条评论) (1 个反应) (1 位负责人)C# (4,257 个派生)batch import
Area-CompilersCode Gen QualityFeature - Collection Expressionshelp wanted

仓库指标

星标
 (20,414 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

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.

贡献者指南