-
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
Initial blittable proposal #206
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
# Blittable Types | ||
|
||
* [x] Proposed | ||
* [ ] Prototype | ||
* [ ] Implementation | ||
* [ ] Specification | ||
|
||
## Summary | ||
[summary]: #summary | ||
|
||
The blittable feature will give language enforcement to the class of types known as "unmanaged types" in the C# language spec. This is defined in section 18.2 as a type which is not a reference type and doesn't contain reference type fields at any level of nesting. | ||
|
||
## Motivation | ||
[motivation]: #motivation | ||
|
||
The primary motivation is to make it easier to author low level interop code in C#. Blittable types are one of the core building blocks for interop code, yet abstracting around them is difficult today due to a lack of declarative language support. | ||
|
||
This is the case even though blittable types are well defined in the language spec and many features, like pointers, can operate only on them. The language chooses to let structs implicitly opt into being blittable by virtue of their construction with no avenue for opting out of such a classification. | ||
|
||
While attractive in small applications this is more difficult to manage across a large set of libraries authored by different developers. It means small field additions to structs can cause compile and runtime breaks in downstream consumers with no warning to the developers that made the change. This spooky action at a distance is one of the core problems with having an implicit opt in model here. | ||
|
||
A declarative, explicit opt in model makes it easier to code in this area. Developers can be very explicit about the types they intend for interop. This gives them compiler help when mistakes are made in their application and in any libraries they consume. | ||
|
||
The lack of compile time support also makes it difficult to abstract over blittable types. It's not possible for instance to author common helper routines using generic code: | ||
|
||
``` c# | ||
void Hash<T>(T value) where T : blittable struct | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: You use I personally prefer just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will make it consistent. My preference is still for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jaredpar can you say "blittable type" to cover all possible blittable types? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @whoisj not quite sure what you are asking there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jaredpar seemed like confusion of using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jaredpar, a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this allow the use of generic pointers? Like: |
||
{ | ||
using (T* p = &value) { | ||
... | ||
} | ||
} | ||
``` | ||
|
||
Instead developers are forced to rewrite virtually the same code for all of their blittable types: | ||
|
||
``` c# | ||
void Hash(Point p) { | ||
... | ||
} | ||
|
||
void Hash(Time t) { | ||
... | ||
} | ||
``` | ||
|
||
The lack of constraints here also make it impossible to have efficient conversions between streams of data and more structured types. This is an operation that is common in networking stacks and serialization layers: | ||
|
||
``` c# | ||
Span<byte> Convert<T>(ref T value) where T : blittable { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the constraint be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Should've been blittable struct. |
||
... | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From recent chat in LDM, the question came up: is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not think
That makes them non-blittable by definition. This optimization would need to be undone for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a large number of generic types, such as With the current proposal, structs will need to be explicitly marked as blittable ( There are already limitation around using generics with any kind of P/Invoke. However, if we had a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
These two are pretty different: Taking address of generic type is simple, well-defined operation. You can do it today using Generics in P/Invokes is not simple and well defined. It is limitation of the PInvoke marshalling baked into the runtime. PInvoke marshalling rules are pretty complex. Throwing generics into the mix would make them even more complex. |
||
``` | ||
|
||
Such routines are advantageous because they are provably safe at compile time and allocation free. Interop authors today can not due this (even though it's at a layer where perf is critical). Instead they need to rely on allocating routines that have expensive runtime checks to verify values are correctly blittable. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "due" -> "do" |
||
|
||
## Detailed design | ||
[design]: #detailed-design | ||
|
||
The language will introduce a new declaration modifier named `blittable` that can be applied to `struct` definitions. | ||
|
||
``` c# | ||
blittable struct Point | ||
{ | ||
public int X; | ||
public int Y; | ||
} | ||
``` | ||
|
||
The compiler will enforce that the fields of such a struct definition fit one of the following categories: | ||
|
||
- `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `char`, `float`, `double`, `decimal`, or `bool`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, Please, Please do not leave off There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was mentioned in the issue as well. It was an ommission, will add before merging. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your list contains quite a few non blittable types, e.g. decimal, char, bool. Are those mistakes or so you plan to change this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I've waffled a bit on whether these should be special cased as implicitly blittable, or waiting for source annotations to happen. Leaning towards implicit.
Been way down this rabbit hole in Midori. The basic problem comes down to whether you believe bilttable means ...
In Midori we actually separated this out into two features: primitive and blittable. All blittable are primitive but not the other way around. The distinction was interesting but IMHO wasn't worth the extra complexity. In any case though, this feature definitely targets 1 above. It's the language manifestation of the existing spec concept of "unmanaged type". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if we are explicitly stating that this is for 'unmanaged types' (that is types for which the unmanaged and managed representation is bit for bit identical) then we should not be including 'char' or 'bool', since the statement is not always true for these types. String is technically blittable in some cases (and there is logic for pinning a string rather than copying in those instances). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tannergooding Thank you both for your detailed explanations. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes, I missed the Auto layout attribute. It might be worth logging bugs to remove that. At least in the case of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Filed bugs https://github.com/dotnet/coreclr/issues/10448 and https://github.com/dotnet/coreclr/issues/10449 for the layoutkind issue |
||
- Any `enum` type | ||
- Any pointer type which points to a `blittable` type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can't this be any pointer? It should not matter what the pointer points to. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then I withdraw my |
||
- Any user defined struct explicitly declared as `blittable` | ||
|
||
Any type fitting the definition above, or any element in the list, is considered a valid `blittable` type. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought you wanted to add a section for fixed arrays as well (#78)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I considered that but as we were discussing I'm not sure why we should limit this to blittable types, seems we can trivially expand it to any type out there. Working to validate if that's legal or not. If it's not then I'll go back and expand this proposal to do that. I should explicitly mention in the proposal though that a fixed array of blittable types is considered a valid field type inside a blittable struct. |
||
|
||
``` c# | ||
struct User | ||
{ | ||
string FirstName; | ||
string LastName; | ||
} | ||
|
||
blittable struct ItemData | ||
{ | ||
// Error: blittable struct cannot contain field of type User which is not blittable | ||
User User; | ||
int Id; | ||
} | ||
``` | ||
|
||
Note that a user defined struct must be explicitly declared as `blittable` in order to meet the requirements above. This is required even if the struct otherwise meets the requirements of `blittable`. | ||
|
||
``` c# | ||
struct SimplePoint | ||
{ | ||
public int X; | ||
public int Y; | ||
} | ||
|
||
blittable struct Data | ||
{ | ||
// Error: blittable struct cannot contain field of type SimplePoint which is not blittable | ||
SimplePoint Point; | ||
} | ||
``` | ||
|
||
The language will also support the ability to constrain generic type parameters to be `blittable` types. | ||
|
||
``` C# | ||
void M<T>(T p) where T : blittable struct | ||
{ | ||
... | ||
} | ||
|
||
M<Point>(); // Ok | ||
M<User>(); // Error: Type User does not satisfy the blittable constraint. | ||
``` | ||
|
||
One of the primary motivations for `blittable` structs is ease of interop. As such it's imperative that such a type have it's field ordered in a sequential, or explicit, layout. An auto layout of fields makes it impossible to reliably interop the data. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: second "it's" -> "its" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its correct I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, rule of thumb I say is if it has a apostrophe, read it in your head as "it is" and see it if makes sense. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jnm2 *and see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simple mneumonic for its/it's: Think of the apostrophe as the dot on an 'i'. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @GaTechThomas mnemonic |
||
|
||
The default today is for a sequential layout so this doesn't represent a substantial change. However the compiler will make it illegal to mark such types as having an auto layout. | ||
|
||
``` c# | ||
// Error: A blittable struct may not be marked with an auto layout | ||
[StructLayout(LayoutKind.Auto)] | ||
blittable struct LayoutExample | ||
{ | ||
... | ||
} | ||
``` | ||
|
||
## Drawbacks | ||
[drawbacks]: #drawbacks | ||
|
||
The primary drawback of this feature is that it serves a small number of developers: typically low level library authors or frameworks. Hence it's spending precious language time for a small number of developers. | ||
|
||
Yet these frameworks are are often the basis for the majority of .NET applications out there. Hence performance / correctness wins at this level can have a riple effect on the .NET ecosystem. This makes the feature worth considering even with the limited audience. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While we're at it, |
||
|
||
There will also likely be a small transition period after this is released where core libraries move to adopt it. Types like [System.Runtime.InteropServices.ComTypes.FILETIME](https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.comtypes.filetime(v=vs.110).aspx) are common in interop code. Until it is marked as `blittable` in source though, developers won't be able to depend on it in their libraries. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a very important feature for Unity, that goes far beyond low level library authors. This is something many of our customers would use. In games generally we very much like the ability to pack our memory in a very controlled way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once we have |
||
## Alternatives | ||
[alternatives]: #alternatives | ||
|
||
There are a couple of alternatives to consider: | ||
|
||
- The status quo: The feature is not justified on its own merits and developers continue to use the implicit opt in behavior. | ||
- Generic constraint only: The blittable keyword is used on generic constraints only. This allows for developers to author efficient helper libraries. But the types involved lack any declarative support and hence are fragile across distributed development. | ||
|
||
|
||
## Unresolved questions | ||
[unresolved]: #unresolved-questions | ||
|
||
- blittable vs. unmanaged. The F# language has a very [similar feature](https://docs.microsoft.com/en-us/dotnet/articles/fsharp/language-reference/generics/constraints) which uses the keyword unmanaged. The blittable name comes from the use in Midori. May want to look to precedence here and use unmanaged instead. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "blittable type" has an existing definition in CLR interop already: https://docs.microsoft.com/en-us/dotnet/framework/interop/blittable-and-non-blittable-types . The existing definition is different from your definition, e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's probably worthwhile to keep this as blittable and remove That being said, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The existing definition also have "The following complex types are also blittable types: One-dimensional arrays of blittable types". What would you do about that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we are going to go with the term "unmanaged type". It is already in use in the C# spec and F#. |
||
- Verifier: does the verifier / runtime need to be updated to understad the use of pointers to generic type parameters? Or can it simply work as is without changes. | ||
|
||
## Design meetings | ||
|
||
n/a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is possible using
System.Runtime.CompilerServices.Unsafe
package.