3 ms·
There are pros and cons. C++ templates can't be reified at run-time like C# generics can.
by nick_ 2y ago
There are pros and cons. C++ templates can't be reified at run-time like C# generics can.
- int_19h 2y agoFWIW for C# this also requires having a JIT (to handle generic virtual methods).
- neonsunset 2y agoGVM dispatch is notoriously slow(-ish), yeah. But it does not require JIT. Otherwise it wouldn't work with NativeAOT :) (the latter can also auto-seal methods and unconditionally devirtualize few-implementation members which does a good job, guarded devirtualization with JIT does this even better however)
- int_19h 2y agoI remember when this feature was specifically not available with NativeAOT. It's good that it is now, but how can it be implemented in a way that has truly separate instantiations of generics at runtime, when calls cross assembly boundaries? There's no single good place to generate a specialization when virtual method body is in one assembly while the type parameter passed to it is a type in another assembly.
- neonsunset 2y ago> how can it be implemented in a way that has truly separate instantiations of generics at runtime, when calls cross assembly boundaries There are no assembly boundaries under NativeAOT :) Even with JIT compilation - the main concern, and what requires special handling, are collectible assemblies. In either case it just JITs the implementation. The cost comes from the lookup - you have to look up a virtual member implementation and then specific generic instantiation of it, which is what makes it more expensive. NativeAOT has the definitive knowledge of all generic instantiations that exist, since it must compile all code and the final binary does not have JIT.