Closed
Description
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.