Ranges and indices of native int (or arbitrary size) #4540
Replies: 3 comments 3 replies
-
Given that .NET can't do arithmetic operations on generic types, how would this and this work? Also, slice syntax requires that the type being sliced implements a |
Beta Was this translation helpful? Give feedback.
-
I'm having a similar problem with MemoryMappedFiles. Here I want to create a Span to the memory but only have Int32 to work with. It would be good to have a Span that supports 64-bit. |
Beta Was this translation helpful? Give feedback.
-
The ecosystem is quite strange here about lengths. The returning IL of array length is nint(you can see a cast inserted by C# compiler when accessing array length), but GC is limiting it to int32 since larger will be very hard or even impossible for GC. In contrast, other types are using int32 for length in IL, but they don't have such hard limit like GC managed array. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The current design of ranges and indices requires
System.Range
andSystem.Index
types to beInt32
based thus further propagating inability to handle large massifs of data in C# with style.In my scenario,
Int32
limitation is hit when trying to slice or index large tensors. As instances ofTensor
point to natively allocated memory that in most cases actually resides on GPU, their dimensions are not subject to the same unfortunate ~4GB size restriction imposed by .NET runtimes on regular .NET arrays allocated from .NET heap(s).Unfortunately, the current design of the feature leaves absolutely no recourse for hacking around to get indices > 32 bit by explicitly requiring
Int32
.I suggest C# language committee to consider extending index, ranges and slicing syntaxes to allow generic
Index<T>
andRange<T>
types with the same public interface as the currentIndex
andRange
types with every use ofInt32
(exceptGetHashCode
) replaced withT
.Though the above option is highly preferred for forward compatibility reasons (smaller types for microcontrollers or larger than
nint
types for manipulating extremely large external databases, or simply manipulating 4+GB data from 32 bit .NET apps), it is also feasible to simply haveNativeIndex
andNativeRange
types, that usenint
instead ofInt32
.Further, I'd be curious if the current language design process can be improved to avoid feature locks like this one.
Beta Was this translation helpful? Give feedback.
All reactions