3 ms·
If you guarantee static dispatch, then you haven't solved the expression problem, which requires some sort of dynamic dispatch (even a switch/pattern match). Th
by pron 11y ago
If you guarantee static dispatch, then you haven't solved the expression problem, which requires some sort of dynamic dispatch (even a switch/pattern match). The only way to address the expression problem and provide zero time overhead in practice (though it can't be guaranteed) is with a JIT/profile-guided optimization.
This, BTW, is why I find Rust's use of the term "zero-cost abstractions" confusing. Where Rust guarantees zero-cost it does so for weak abstractions only, and where it provides stronger abstractions, their cost may be higher than in other languages (because Rust's compiler is still behind on optimizations). It's a little like a restaurant advertising "free food", when they're really only offering free popcorn at the bar, while their steaks are rather pricey. It may be a great offering, but the copy is a little confusing.
- kibwen 11y agoHey, I'm not the one claiming that Rust solves the expression problem. :) > where it provides stronger abstractions, their cost may > be higher than in other languages What is this referring to? I can't think of anything in Rust that matches this description, unless you're referring to something like e.g. devirtualization via JIT compilation. Rust's abstractions are actually supremely impressive IMO; just yesterday I was marvelling at how `(0..x).fold(0, |sum, i| sum + i)` (which involves an external iterator, a generic function, and a closure) gets compiled down to the closed-form `((x - 1) * (x - 2) / 2) + x - 1` (which admittedly slightly misses Gauss' famous `(x * (x + 1)) / 2`, but that might just be nitpicking :P ).
- pron 11y ago> unless you're referring to something like e.g. devirtualization via JIT compilation. Yep. > I was marvelling at how `(0..x).fold(0, |sum, i| sum + i)` (which involves an external iterator, a generic function, and a closure) gets compiled down to the closed-form `((x - 1) * (x - 2) / 2) + x - 1` That is very cool, but once iterators are turned into loops (perhaps they're even treated specially) and the closure inlined (I would expect both from a decent compiler), it is a rather straightforward optimization (a loop with a constant step). OTOH, a JIT can do this: https://twitter.com/ChrisGSeaton/status/619885182104043520 https://twitter.com/ChrisGSeaton/status/619885182104043520
- kibwen 11y ago> Yep. Unlike some other languages, the vast majority of calls in Rust are static rather than dynamic (even moreso than C++), which averts the problem from the outset. And even then, LLVM is still capable of AOT devirtualization. It's very rare to reach for a trait object rather than a bounded generic, so I'm afraid that categorizing bounded generics as a "weak abstraction" is hyperbolic on your part. :) > a JIT can do this Constant folding is hardly the exclusive purview of JIT. :P When coming up with the above example I had to repeatedly jump through hoops in order to keep LLVM from statically evaluating the entire function; even when I managed to force LLVM to stop inlining the function, it went behind my back and propagated the constant into the uninlined function, creating a symbol that did nothing other than return the integer 5050. :P
- pron 11y ago> I'm afraid that categorizing bounded generics as a "weak abstraction" is hyperbolic on your part. Yeah, probably. > Constant folding is hardly the exclusive purview of JIT. Oh, sure, but in that example, an AOT would have a hard time doing that (certainly without special rules).
- hokkos 11y agoRust provides dynamic dispatch through trait objects : https://doc.rust-lang.org/book/trait-objects.html https://doc.rust-lang.org/book/trait-objects.html It provides it with a simple vtable like other languages, with the same efficiency. And PGO is possible with the use of LLVM, but not easily for now : https://github.com/Geal/pgo-rust https://github.com/Geal/pgo-rust
- mwcampbell 11y agoDoes Rust at least make the stronger abstractions as low-cost as they can be with no JIT compiler?
- pron 11y agoOh, it certainly could (I don't know whether it currently does, simply due to compiler maturity), but my "complaint" isn't about Rust's abilities but its use of the term zero cost abstractions.
- steveklabnik 11y agoThe phrase was taken from C++, and refers to the implementation of said abstractions. If you built them yourself, you couldn't have built them better. This still presumes things like an AOT model, but that's what it's meant to call back to, not literally everything in every situation ever.