3 ms·
Reified Generics?
by scns 6y ago
Reified Generics?
- est31 6y agoYeah that's the formal term. Thank you. Languages like Java have erased generics where you can't even put something of type T onto the stack because the generated code has to stay generic on the size of T. Super annoying to work with. On the other hand, reified generics increases the binary size. The advantage is that this is more digestible for the optimizer for inlining and specific optimization, which gives you more predictable performance than the alternative of devirtualization, but of course size can also have negative consequences, like for example slower compile time. One good example is this recent PR that improved compile time by making parts of the Vec implementation in the standard library non-generic: https://github.com/rust-lang/rust/pull/72013 https://github.com/rust-lang/rust/pull/72013
- jen20 6y agoI was very much under the impression that Rust used monomorphization, and that there was no concept of generics at runtime (unlike, say the CLR). Am I missing something here?
- zozbot234 6y agoRust supports both monomorphized and type-erased implementations, the latter via the `dyn` keyword when instantiating a trait (similar to an interface in Java).
- jacoblambda 6y agoThey support both but general rule of thumb I've found from the community is use the prior by default unless you have to use the latter.
- Sharlin 6y agoOr monomorphized generics.