-
Notifications
You must be signed in to change notification settings - Fork 797
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
Struct discriminated unions with explicit layout for more compact size #9368
Comments
An interesting request, but since a struct DU internally requires more data, afaik mainly to store the Perhaps a better option in your case is to use It does make one wonder, btw, why a struct-DU has to have the size of all its members (plus 4), esp if the type is blittable. I don't know enough of the inner complexities, but it seems reasonable to assume that a struct-DU allows for overlapping fields by default (or at user option), since at any given time only one field will be set. For instance: take the largest single blittable field, add 4 bytes for the This method of overlap is applied for normal DU's, though (using polymorphism internally), meaning in many cases the struct-DU will end up being larger than the class-DU (but on the heap). |
@abelbraaksma well this is sort of what I meant, maybe I didn't explain it clearly. Currently struct DU is implemented as a |
@isaevdan, while this is true for the way it is coded up, it is not true for the internal memory layout after JIT'ing. In fact, in almost all cases, the JIT will rearrange the layout to something more useful. For instance, if you have, say, This process is dependent on CPU architecture and it is possible it is different for different JITs. In other words, how it is laid out in code is irrelevant w.r.t. optimizations. My suggestion, however, was to let the blittable types overlap, while still maintaining padding and alignment. Whether this is feasible, and/or whether this leads to better or worse performance is something I cannot yet answer ;). Once you use
As a hint, partial matching patterns are executed for each match, complete active patterns (without |
@abelbraaksma Thanks for this insight. As I understand in case of struct DU, only 2 fields are needed to be located in memory |
FYI, related: #5215 |
I always avoid creating struct DUs mainly because of how large the struct can become. I think a reasonable first step is the merging of fields of the same type. |
Covered by the language suggestion |
I'm currently working on project that requires efficient memory management and we do have a base type which is created millions of times during of application lifecycle. For now we use reference DU, but this actually leads to performance drop as we need to create lots of small objects all the time.
We consider to move to struct DU to avoid heap allocation, but because of native implementation doesn't use explicit layout and our DU has 5 cases it will result in quite big object while only small part of it will be used. This may result in worse performance. So we will probably look into some custom implementation with layouting, but we will lose pattern matching feature and will have to do a major refactoring.
Is there any reasoning why struct DU wasn't implement with Explicit layout? Is it possible to add such feature in future?
The text was updated successfully, but these errors were encountered: