-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal]: First class native integer support #4385
Comments
For multi-dimensional or non-zero based arrays, the spec makes no mention of needing to use If overloads taking |
The "Pointer member access" section should probably be renamed to "Pointer element access" and refer to https://github.com/dotnet/csharplang/blob/master/spec/unsafe-code.md#pointer-element-access instead. |
Updated, I must have missed which was the nearest header 😄 |
How about support for |
I think this needs to go through the runtime first to see if they actually want a different API here. If they do, then we can consider if in the language level it's something we want first class support for. |
@CyrusNajmabadi does runtime provide any special support for the Performance wise runtime can catch up later. |
For language features that require APIs they are only going to target those supplied by the runtime for the supported compiler and runtime version. Yes, you can polyfill older runtimes, but that's no longer considered supported, and it's very unlikely that the language would ship with a language change that would require a polyfill. |
@lostmsu It's not so much that the types are tightly coupled to the runtime. The issue is with the APIs and implementations of those types, which are provided by the runtime (where they're built in), or in your case by the nuget package. In both cases, the API and implementations for Notice how the proposal only modifies parts of the language that currently apply to any of |
What is the reasoning behind that? I used Unlike @naine and that's why I'd prefer a new |
I believe that the language team doesn't want to get into the situation of having to describe which features depend on runtime support and which features can be polyfilled, so the official stance is that the language and runtime will evolve in lockstep and that down-level targeting is unsupported. That stance started with C# 8.0 and .NET 3.0 due to Default Interface Methods. For that reason I doubt that the language team would consider adding features that depended on APIs unless the supporting APIs were scheduled to ship in the corresponding runtime release. |
First class native integer support
Speclet: https://github.com/dotnet/csharplang/blob/main/proposals/csharp-11.0/numeric-intptr.md
Summary
Native integers should be fully integrated into the language such that they are "first class citizens". They are currently missing out in areas where a
native integer
is the logical type to use such as inArray creation
,Array element access
,pointer conversions
,pointer element access
, andpointer arithmetic
.Motivation
Native integers are implicitly supported in many of these scenarios today as they have an implicit conversion to
long
orulong
which is explicitly supported in said scenarios. However,long
/ulong
are "too large" on 32-bit platforms and as such their usage generally involves an explicit overflow check leading to less efficient code generation, particularly when most underlying platforms eventually convert tonint
/nuint
anyways as that is the required size too address memory.Detailed design
Array Creation expressions
https://github.com/dotnet/csharplang/blob/master/spec/expressions.md#array-creation-expressions should be reworded to:
Array element access
https://github.com/dotnet/csharplang/blob/master/spec/arrays.md#array-element-access should be reworded to:
https://github.com/dotnet/csharplang/blob/master/spec/expressions.md#array-access should be reworded to:
Pointer types
https://github.com/dotnet/csharplang/blob/master/spec/unsafe-code.md#pointer-types should be reworded to:
Pointer conversions
https://github.com/dotnet/csharplang/blob/master/spec/unsafe-code.md#pointer-conversions should be reworded to:
Pointer element access
https://github.com/dotnet/csharplang/blob/master/spec/unsafe-code.md#pointer-element-access should be reworded to:
Pointer arithmetic
https://github.com/dotnet/csharplang/blob/master/spec/unsafe-code.md#pointer-arithmetic should be reworded to:
Drawbacks
It adds additional complexity to the language that could potentially be handled in another fashion.
Alternatives
The impact of not doing this is that the backing code generated by the compiler is typically worse because
long
andulong
are typically not valid indexers on the underlying host platform, meaning the C# compiler must account for this and insert additional overflow checks.However, it could also be handled by updating the emitter to account for
nint
andnuint
conversions tolong
andulong
. There may also be some additional optimizations possible in the JIT to allow folding away unnecessary overflow conversions.Unresolved questions
It doesn't appear as though the language spec has been updated to include the
nint
andnuint
additions from C# 9, so it is unclear if a few of the suggestions above are already handled. Likewise, there are some additional locations throughout the spec that should be validated to covernint
andnuint
.In
Pointer arithmetic
, there exists the following operatorlong operator -(T* x, T* y);
. The logical type if this was done from C# v1.0 should have beennint
, which is the equivalent type ofptrdiff_t
on C/C++ platforms. The rules on how to add this without breaking compatibility are somewhat unclear to me. It may be possible that we just addnint operator -((T* x, T* y)
but that is "effectively" overloading based on return type and whilenint
is in turn implicitly convertible tolong
, there may be edge cases I'm unaware of that would prevent this.Design meetings
The text was updated successfully, but these errors were encountered: