Ask HN: Do C++ templates result in a combinatorial explosion?
2. Does the parametric polymorphism of Rust result in the same combinatorial explosion? Does Rust's parametric polymorphism just generates ad-hoc polymorphic code?
2. Does the parametric polymorphism of Rust result in the same combinatorial explosion? Does Rust's parametric polymorphism just generates ad-hoc polymorphic code?
Instantiating the same template the same way in multiple compilation units can get you another multiplicative factor. I’m not sure if link time optimization de-duplicates the result or not.
The main practical problem with this (other than big binaries) is that you can exhaust the instruction cache on your machine, which slows things down significantly.
I’m not sure how rust code generation works.
Apparently, one of the central design tenants of swift was to keep the compiled binary small, since it preserves instruction cache. This matters a lot when you have many different programs running (such as on an iPhone). Also, instruction cache thrashing is terrible for energy efficiency.
But do ALL implementations of parametric polymorphism boil down to "erasing and replacing" with generated code?
For instance, in Haskell you can define a type such as
data Tree a = Single a | Bigger (Tree (Node23 a))
data Node23 a = Node2 a a | Node3 a a a
And that will basically look like a linked list Bigger (Bigger (Bigger ...)) until it hits a fixed-depth 2,3-tree (for some depth) whose type might look something like Node23 (Node23 (Node23 (Node23 Int))).To actually recurse over the Node23 in code, you need type classes. It works just fine. The executable has finite size, and the type class instances you'd use would presumably get constructed on the fly, holding a reference to smaller types' instances.
I'm not sure if that answers your question.
Basically do all languages with parametric polymorphism do that?