All abstractions made using types are monomorphized so they have zero impact at runtime. In the output assembly there is no leftover trace of their existence.
"zero cost abstraction" guarantees that the abstracted version is equal in terms of cost to a non abstracted specific implementation.
"zero impact at runtime" might be a bit much. there is no *guarantee* that "zero cost abstractions" are equal to non abstracted versions, it is only true if the optimizer works good enough (and it often can't!)
in this case abstractions add a lot of nested function calls, which make it harder to optimize. llvm is still good at it, but abstractions do make compilation slower and the result potentially, depending on how the optimizer feels, also slower (or less size efficient).
Do these interfaces add a lot of nested calls tho? I thought they just add additional runtime checks (which are non zero but still light) and compile time checks. And thats it?
I said it was guaranteed because rust is trying to guarantee it. If an optimization is possible and does not happen it is usually treated as a bug in the compiler. There has been a fair bit of work on optimising the MIR to reduce the burden on LLVM to figure out the optimizations for the abstraction
In this case there’s no indirection. Every inner type is a concrete static type. There’s no runtime that checks which functions are there. Just function addresses to the statically compiled functions.
Comments
"zero cost abstraction" guarantees that the abstracted version is equal in terms of cost to a non abstracted specific implementation.