Rust zero-cost abstractions vs. SIMD

24 points by emschwartz


ambee

A zero-cost abstraction promises that we couldn't hand-write faster assembly for the same logic.

Nobody who knows what they're talking about has ever said this. Not once. Never.

It only means the Iterator abstraction has no inherent runtime cost over the alternative natural expression of the logic in the language. Assembly is not Rust, Rust does not make claims in relation to what you can do with assembly.

cceckman

This was an interesting read!

Nicole's post and my followup show that LLVM can inline a next call, and flatten iterators... when it feels like it. I guess it didn't inline in this case because of the complexity of next: "Each call to next() compares across iterators, mutates internal positions, and returns one value."

Which aligns with the broader point of the article: "zero-cost abstractions do not absolve you from practicing mechanical sympathy." Totally agreed!

I don't think I'd understood "zero-cost abstraction" as "couldn't hand-write faster assembly for the same logic". Specifically "assembly"- lowering to assembly implies embedding assumptions/choices that weren't in the original code (choice of compiler, ISA, etc.) (unless you're using intrinsics, which...seems different.)

In the case of an iterator, the iterator abstraction is "just" covering the while-next loop. Function calls are not a zero-cost abstraction; they are an abstraction.

bsdinis

One thing that may be of note is that this implementation could be done by specializing the (nightly only) next_chunk method on Iterator. Which would be nice in terms keeping good Iterator ergonomics.

(This of course is unrelated to the point of zero-cost)

olliej

I’d like to know what their next() functions are actually doing - certainly it seems significantly more complex than normal iterators but given it can apparently be autovectorised when written as a normal loop I’m wondering what the iterator version is actually doing