In Crux, static dispatch is the default, which has two main benefits with regards to performance.
- The JIT always knows precisely which function is being called, so it doesn’t need to dynamically look it up.
- Ahead-of-time optimizers can inline the function if appropriate, meaning lightweight functions are free - a cost-free abstraction.
Static dispatch is one component of performance - another is type-indexed values.
Crux’s traits allow selecting the appropriate function for each type.
Consider the deep equality example above. Obviously the
works on primitive types like Number and String. But it also works on
[0, 1] == [0, 1]. At compile time, Crux notices that it’s comparing
arrays and looks up the
== operator implementation for arrays.
This helps the JIT generate the fastest code for equality checks,
while the programmer is able to use the same
== operator on every