4 ms·
The point about templates being easy to optimize compared to other generics systems is simply wrong. Other generics systems provide more information to the comp
by bbatha 10y ago
The point about templates being easy to optimize compared to other generics systems is simply wrong. Other generics systems provide more information to the compiler, so in addition to compiling faster and providing better error messages the compiler can use knowledge of specific type classes, interfaces, modules to optimize the implementation of. For instance, Rust can optimize `type NonZeroOption<T: NonZero> = Option<T>` to just the size of `T` because it knows that the `None` sentinel value is not used by `T`. You can also have nice features like GHC's rewrite rules which allow you to effectively tell the compiler how to optimize your types.
Most of the fast binary parts of templates is from stuff like specialization, which can be added to just about any generics system. Rust and Haskell both have it.
- dbaupp 10y agoI don't think the T: NonZero bound is doing anything there: the size optimisation is done on the monomorphised/specialised type, not the generic one. In Rust, the concept of a generic types only exist at compile time, and they have no layout. Of course, this is still an example of a generic systems that specializes for performance, in a language that isn't C++.
- gpderetta 10y ago> nice features like GHC's rewrite rules C++ templates, in essence, are rewrite rules. And they can be used to optimize types.
- soulbadguy 10y ago> Other generics systems provide more information to the compiler, so in addition to compiling faster and providing The templating system in C++ is not generic type system, it's not even strictly speaking part of the C++ type system. It's usually define as a metaprogramming facility. Because it be used to abstract on types, it's the defacto generic system in C++, but it can also be used to abstract over definitions, expressions, computation shapes (like unrolling a loop on a factor relative to the size of a type). C++ templates are slow when compared to other generic system because of a lot of bad design decisions (compilation model and how/when a template has to be intanciated), but also because it can do more, and is used to do more. > the compiler can use knowledge of specific type classes, interfaces, modules to optimize the implementation of. Depends on what you mean by more information: In c++ complex type constraints can be express through template. Even more important, in you example you have to rely on someone in the rust compiler community to implement that specific type transformation. Because, the template system is extremely expressive,whoever designed the "option type" can also ship instructions to the compiler on how to specialize for NonZero type... > You can also have nice features like GHC's rewrite rules which allow you to effectively tell the compiler how to optimize your types. It's hard to have both, most generic type system are fast because they don't do much as you start adding feature and smartness into the system, the performance degrades...scala compile time being a good example. > Rust and Haskell both have it. can you specialize on struct in rust/haskell or just implementation of traits ?