While System.Array API supports LongLength and operator this[long i], the CLR does not allow arrays to be allocated with more than 2^31-1 elements (int.MaxValue).
This limitation has become a daily annoyance when working with HPC or big data. We frequently hit this limit.
Why this matters
- .NET arrays is the de facto storage unit of many APIs & collections.
- Currently one has to allocate native memory instead, which does not work with managed code (Span could have helped, but it's been limited to int32 as well).
- HPC frameworks expects data to be contiguous in memory (i.e. the data cannot be split into separate arrays). E.g. BLAS libraries.
- Requires applications to be coded and designed differently if they handle <2G elements or >2G elements. I.e. It does not scale.
- It's an arbitrary limitation with little value to the user and application:
- With a desktop with 64GB, why can one only allocate 3.1% of its memory in one go?
- With a data centre machine with 1,536GB, why can one only allocate 0.1% of its memory in one go?
In C++ this is solved with std::size_t (whose typedef changes depending on the target platform). Ideally, .NET would have taken the same route when designing System.Array. Why they haven't is a mystery, given that AMD64 and .NET Framework appeared around the same time.
Proposal
I suggest that when the CLR/JIT runs the .NET application in x64, it allows the array long constructor to allocate more than int.MaxValue items:
- Indexing the array with operator this[long i] should work as expected and give access to the entire array.
- Indexing the array with operator this[int i] should work as expected but implicitly limit the access to only the first int.MaxValue elements.
- The LongLength property should return the total number of elements in the array.
- The Length property should return the total number of elements in the array, or throw OverflowException of there are more than int.MaxValue elements (this matches the current behaviour on multi-dimensional arrays with more than int.MaxValue elements).
I naively believe that the above should not break any existing application.
Bonus points for extending 64-bit support to Span and ReadOnlySpan.
While System.Array API supports LongLength and operator this[long i], the CLR does not allow arrays to be allocated with more than 2^31-1 elements (int.MaxValue).
This limitation has become a daily annoyance when working with HPC or big data. We frequently hit this limit.
Why this matters
In C++ this is solved with std::size_t (whose typedef changes depending on the target platform). Ideally, .NET would have taken the same route when designing System.Array. Why they haven't is a mystery, given that AMD64 and .NET Framework appeared around the same time.
Proposal
I suggest that when the CLR/JIT runs the .NET application in x64, it allows the array long constructor to allocate more than int.MaxValue items:
I naively believe that the above should not break any existing application.
Bonus points for extending 64-bit support to Span and ReadOnlySpan.