You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are cases where static dispatch is faster than dynamic and there are cases where dynamic can be faster than static. It would be worth noting what the impact of both is.
ex:
Dynamic Dispatch implies at least a vtable lookup, and usually an additional allocation (I think you can do stack dyns now? Maybe not). Static Dispatch can increase icache pressure.
An alternative when you actually need multiple types implementing a single trait would be to use an enum, which can compile down to a jump table. Jump tables can increase pressure on the instruction cache though.
The text was updated successfully, but these errors were encountered:
In Rust, static dispatch is implemented using generics, while dynamic dispatch is implemented using trait objects.
Performance area in dynamic dispatch is vtable lookup and trait object allocations. Implies this is not favorable for huge types. But for unknown types, this might become necessary. Enums might be better alternatives to it in some cases (for more info see the enums video of @ThePrimeagen).
Static dispatch can increase instruction cache (icache) pressure, especially for huge generic types. Because the code for each type is duplicated at compile time, leading to larger binary sizes and more cache misses.
There are cases where static dispatch is faster than dynamic and there are cases where dynamic can be faster than static. It would be worth noting what the impact of both is.
ex:
Dynamic Dispatch implies at least a vtable lookup, and usually an additional allocation (I think you can do stack dyns now? Maybe not). Static Dispatch can increase icache pressure.
An alternative when you actually need multiple types implementing a single trait would be to use an enum, which can compile down to a jump table. Jump tables can increase pressure on the instruction cache though.
The text was updated successfully, but these errors were encountered: