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?
-
-
Replying to @froydnj
I used to feel this way, then joined Team impl Trait, which is ergonomically very similar to Trait objects without the object safety or performance issue of trait objects.
1 reply 0 retweets 3 likes -
until you find out how much worse the compile time is when you monomorphize all the things
1 reply 0 retweets 2 likes -
Replying to @withoutboats @froydnj
We should be able to do better. Many monomorphizations aren't creating very different functions.
1 reply 0 retweets 0 likes -
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.
2 replies 0 retweets 3 likes -
Replying to @TedMielczarek @wycats and
It's called "outlining" and yeah the compiler should be able to help here.
1 reply 0 retweets 3 likes -
Replying to @ManishEarth @TedMielczarek and
fwiw I don't necessarily think impl Trait will make this much worse. Generics are already greatly preferred to trait objects culturally in rust.
1 reply 0 retweets 0 likes -
Replying to @wycats @TedMielczarek and
Impl trait in input position is just sugar, so it changes nothing. In an output position it avoids boxing is all.
1 reply 0 retweets 0 likes -
Replying to @ManishEarth @TedMielczarek and
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.
1 reply 0 retweets 1 like -
Replying to @wycats @ManishEarth and
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.
1 reply 0 retweets 2 likes
It helps that this is compile time performance and not runtime perf.
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.