Contextual Dispatch for Function Specialization


Article by Olivier Flückiger, Guido Chari, Ming-Ho Yee, Jan Jecmen, Jakob Hain, Jan Vitek, published at OOPSLA ‘20.

In order to generate efficient code for dynamic languages, compilers often need information not readily available in the source code. Leveraging a mixture of static and dynamic information, just-in-time compilers can speculate on the missing information. Within one compilation unit, code is specialized to the observed behaviors. We propose an approach to further the specialization, by disentangling classes of behaviors into separate optimization units. With contextual dispatch, functions are versioned and each version is compiled under different assumptions. When a function is invoked, the implementation dispatches to a version that was optimized under assumptions matching the dynamic context of the call. As a proof-of-concept, we describe a compiler for the R language which uses this approach. We evaluate contextual dispatch on a set of benchmarks and compare to traditional speculation with deoptimization techniques. Our implementation is, on average, 1.7× faster than the GNU R reference implementation, and contextual dispatch contributes to the performance significantly in 18 of 46 programs in our benchmark suite.