dotnet/roslyn

No ReadOnlySpan<byte> static data optimization for new byte[N]

オープン

#33,088 opened on 2019/02/04

 (12 件のコメント) (1 件のリアクション) (1 人の担当者)C# (4,257 件のフォーク)batch import
Area-CompilersFeature Requesthelp wanted

Repository metrics

Stars
 (20,414 個のスター)
PR merge metrics
 (平均マージ 6d 17h) (30d で 256 merged PRs)

説明

Version Used: dotnet 2.2.200-preview-009748

Steps to Reproduce:

Declare a static ReadOnlySpan<byte> property of the form:

public class C
{
    public static ReadOnlySpan<byte> StaticData => new byte[] { 0 };
    public static ReadOnlySpan<byte> DoesntWork => new byte[1];
} 

Expected Behavior:

Per the optimisation in #24621, I expected that both StaticData and DoesntWork would be compiled to a reference to static data, e.g., new ReadOnlySpan<byte>(&global::<PrivateImplementationDetails>.5BA93C9DB0CFF93F52B521D7420E43F6EDA2784F, 1);

Actual Behavior:

Only StaticData has the implementation optimisation; DoesntWork doesn't, even though it has the exact same data. The problem is more noticeable with an expression like new byte[1000], where you might not want to type out 1000 literal zero bytes in source code.

SharpLab: https://sharplab.io/#v2:D4AQDABCCMDcCwAoJIDMUBMEDCSDeSERU6MAbBAEoCmAhgCYDyAdgDYCeAygA63MA8AI3YAXagD4InEbREBLAMYARWbQgBeSc2oB3CMLEBtALoQ8ESAF8EiYiSjQKNBiw48+Q0RIhKA9tQBnZhEAdV8AJwBrDS1dfS9DaGMbSwggA===

コントリビューターガイド