Skip to content

[API Proposal]: Add read only collection returning members to NumberFormatInfo and DateTimeFormatInfo to avoid allocation of cloning #85034

@huoyaoyuan

Description

@huoyaoyuan

Background and motivation

Currently, many members in CultureInfo and NumberFormatInfo returns array. Because the lack of immutable array in the first play, these members have to return a copy of the array. To avoid the allocation, many members in CoreLib are accessing the storage directly and bypassing the copy (For example #84964). However, types outside CoreLib can't benefit from this. This is currently mainly affecting BigInteger.

API Proposal

namespace System.Globalization;

public sealed class NumberFormatInfo
{
    public int[] CurrencyGroupSizes { get; set; }
+    public ReadOnlySpan<int> CurrencyGroupSizesValue { get; }
    public int[] NumberGroupSizes { get; set; }
+    public ReadOnlySpan<int> NumberGroupSizesValue { get; }
    public int[] PercentGroupSizes { get; set; }
+    public ReadOnlySpan<int> PercentGroupSizesValue { get; }
    public string[] NativeDigits { get; set; }
+    public ReadOnlySpan<string> NativeDigitsValue { get; }
}

public sealed class DateTimeFormatInfo
{
    public string[] AbbreviatedDayNames { get; set; }
+    public ReadOnlySpan<string> AbbreviatedDayNamesValue { get; }
    public string[] AbbreviatedMonthGenitiveNames { get; set; }
+    public ReadOnlySpan<string> AbbreviatedMonthGenitiveNames Value { get; }
    // ... more similar member omitted
}

API Usage

Replacing the following direct access of storage array in CoreLib:

int[] groupDigits = info._numberGroupSizes;

Alternative Designs

Use different return types like IReadOnlyList<T>? ImmutableArray<T> isn't an option here since it doesn't live in CoreLib.

Are these members, especially DateTimeFormatInfo used wide enough outside CoreLib?

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions