-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Description
Initialization of literal arrays like new int[]{1,2,3,4,5,6} has a special path where the blittable data is stored directly in the PE data and at runtime, instead of assigning every element of the array to a corresponding constant, we call InitializeArray with the target array instance and the token for the field that represents the data blob.
Similar technique would be very useful when initializing spans.
We already have two scenarios:
- stack allocated spans -
stackalloc int[] {1,2,3,4,5} - optimized conversion of literal arrays to ReadOnlySpan -
(ReadOnlySpan<int>)new int[]{1,2,3,4,5}
in fact in the case with ReadOnlySpan conversion, it would be possible and desirable to just refer to the PE data directly.
The preferred form of the API would be:
ReadOnlySpan<T> GetGetReadOnlySpanFromTemplate<T>(RuntimeFieldHandle fldHandle);Another acceptable alternative is:
(but the one above feels more convenient since verifiability is less of a problem)
ref T GetRefToTemplateData<T>(RuntimeFieldHandle fldHandle);A valid question to be asked here - "if it is possible to just load a reference to the field in the first place, why there is a need for the API?"
The problem is that the blob data is always stored in littleendian format, so on a bigendian machine the blob data is valid only for 1-byte sized elements.
Similarly to the case of InitializeArray, this API would allow the runtime to abstract away the endianness of the blob.
In a littleendian context (which is the most common case) the implementation could trivially forward to the field data and in bigendian case it may do fixups by either making a copy of the data while changing the endianness or even by performing the fixup in-place.
NOTE: possibility of in-place fixup would require that the same blob is not used to initialize span data of different sizes - say shorts and longs.
Such restriction would be acceptable on the C# side and runtime could validate that such "sharing" did not happen, or make it undefined behavior if that happens.
NOTE: the presence of the API is statically known to the compiler, so it would be ok if some runtimes do not have it right away or never. Then optimization will simply not consider 1+ element sizes.