Skip to content

API proposal: Make ImmutableArray readonly struct #334

Closed
@huoyaoyuan

Description

@huoyaoyuan

Background

It's intuitive to use ImmutableArray<T> as readonly fields, and wrap it into other readonly structs. Currently it's not marked readonly, and may cause hidden copies. Although being small itself, immutable arrays are aggresively optimised for performance. Hidden copy of only 1 pointer size can be reduced.

Proposed API

namespace System.Collections.Immutable
{
-    public struct ImmutableArray<T>
+    public readonly struct ImmutableArray<T>
}

Works to do

The only write usage of its field is ImmutableInterlocked. Given that the layout is the prerequisite of interlocked to work, they should be fine to use Unsafe.As<ImmutableArray<T>, T[]>.

Seems that the Unsafe class came later than readonly structs. I guess it's the reason not using it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions