3 ms·
> 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)` (whic
by 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).