AST Specialisation and Partial Evaluation for Easy High-Performance Metaprogramming

The Ruby programming language has extensive metaprogramming functionality. Unlike most similar languages, the use of these features is idiomatic and much of the Ruby ecosystem uses metaprogramming operations in the inner loops of libraries and applications. The foundational techniques to make most of these metaprogramming operations efficient have been known since the work on Smalltalk and Self, but their implementation in practice is difficult enough that they are not widely applied in existing implementations of Ruby and other similar languages. The Truffle framework for writing self-specialising AST interpreters, and the Graal dynamic compiler have been designed to make it easy to develop high-performance implementations of languages. We have found that the tools they provide also make it dramatically easier to implement efficient metaprogramming. In this paper we present metaprogramming patterns from Ruby, show that with Truffle and Graal their implementation can be easy, concise, elegant and highly performant, and highlight the key tools that were needed from them.