Skip to content

[API Proposal]: Predefined InlineArrayX<T> types up to some X #111973

@333fred

Description

@333fred

Background and motivation

Today, Roslyn will emit anonymous inline array types when users call methods that take params ReadOnlySpan in params form, or when using collection expressions in some fashion. There can be a significant number of these types; for example, @jaredpar found that in Microsoft.AspNetCore.App, there are ~110 such types. In Microsoft.NetCore.app, there are ~136 such types. Most of these types are 5 elements or smaller, thus I'm only proposing up to InlineArray5. However, we may want to consider going further; dotnet/roslyn#74538 asks for Roslyn to emit calls to string.Concat(ReadOnlySpan<string>), which may push the numbers here up more. Note that Roslyn will continue to use the discrete string.Concat(string, string, ...) methods where possible, so if we only do up to 5, then the benefit to string.Concat may be minimal. I'll leave it to the BCL designers to decide what number is right, and Roslyn will take advantage of whatever is available.

API Proposal

namespace System.Runtime.CompilerServices;

[InlineArray(1)]
public struct InlineArray1<T>
{
    private T t;
}
[InlineArray(2)]
public struct InlineArray2<T>
{
    private T t;
}
[InlineArray(3)]
public struct InlineArray3<T>
{
    private T t;
}
[InlineArray(4)]
public struct InlineArray4<T>
{
    private T t;
}
[InlineArray(5)]
public struct InlineArray5<T>
{
    private T t;
}

API Usage

var finalString = string.Concat(str1, str2, str3, str4, str5); // The compiler uses InlineArray5<string> behind the scenes

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Runtime.CompilerServicesin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions