Why does idiomatic Rust code pervasively parameterize over trait bounds, rather than simply taking trait objects? The speed argument smacks of premature optimization; what are the other reasons?
We should be able to do better. Many monomorphizations aren't creating very different functions.
-
-
I thought someone also proposed that the compiler should be able to handle simple cases like `fn foo<T: AsRef<str>>(t: T) {}` where it could generate simple generic functions that call a concrete function `fn foo(t: &str) {}` with the actual code.
-
It's called "outlining" and yeah the compiler should be able to help here.
-
fwiw I don't necessarily think impl Trait will make this much worse. Generics are already greatly preferred to trait objects culturally in rust.
-
Impl trait in input position is just sugar, so it changes nothing. In an output position it avoids boxing is all.
-
It could possibly cause people to "use generics more" but I doubt it will move the needle that much. People use the hell out of generics.
-
The main problem with the "premature optimization" argument is that this kind of stuff is hellishly hard to find out from traces. Inefficient dispatch and similar problems make "peanut butter profiles"; they smear a little bit all over the profile.
-
It helps that this is compile time performance and not runtime perf.
End of conversation
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.