Replies: 2 comments 8 replies
-
I think people will be very confused to find that it works for struct unions but not class unions. Also, will that work for AOT shared generics? |
Beta Was this translation helpful? Give feedback.
6 replies
-
I have proposed a general solution to this and a broader range of situations in the runtime repo (dotnet/runtime#105923), but I'd definitely appreciate seeing this addressed! |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm opening this discussion to provide some feedback on the TypeUnions proposal.
In
csharplang/proposals/TypeUnions.md
Line 336 in dcea688
we have
Today we are specializing code for value-type type parameters, so that
IsType<A>
will be specialized to its own code anyway.Given this fact, here we can ask the JIT for some help: we can change the JIT to generate additional code for testing type against the union, only for specialized code whose type arguments are union structs.
Do note that this doesn't mean we are relying on the JIT impl details to implement the type testing. In general, we need to support union structs for all cases involving type testing, here what I'm talking about is an optimization that JIT can be smart so that it won't regress cases that are not involving union structs.
Implementation
The C# compiler emits an attribute
[TypeUnionCase(typeof(<containing union struct>))]
for union struct cases. Also it emits a generic version ofTryGet<T>
for use by the JIT.Say now we have
It got lowered to
When the JIT generates code for a method
Foo<A>
, it looks forTypeUnionCaseAttribute
on the type argumentA
whenA
is a value type. If the attribute is found, in the import stage, the JIT fetches the union typeU
from the attribute and inserts additional code to do type test against the union so that the codebecomes
while for others like
Foo<B>
whereB
is a non-union struct or a class, the codegen won't be changed so the performance of type test against non-union types won't be regressed.We may also want to update the implementation of
Type.IsSubclassOf
to support this forSystem.Type
.Type.IsSubclassOf
is a JIT intrinsic so we can do this smart without regressing performance for most cases while providing fallback code for runtime that doesn't support this intrinsic.Conclusion
In this way we don't need to change the code emitted by the C# compiler, also we don't regress the performance for cases other than union structs.
Beta Was this translation helpful? Give feedback.
All reactions