Copy-and-patch compilation: a fast compilation algorithm for high-level languages and bytecode
Authors:
Open Access
- 15 October 2021
- journal article
- research article
- Published by Association for Computing Machinery (ACM) in Proceedings of the ACM on Programming Languages
- Vol. 5 (OOPSLA) , 1-30
Abstract
Fast compilation is important when compilation occurs at runtime, such as query compilers in modern database systems and WebAssembly virtual machines in modern browsers. We present copy-and-patch, an extremely fast compilation technique that also produces good quality code. It is capable of lowering both high-level languages and low-level bytecode programs to binary code, by stitching together code from a large library of binary implementation variants. We call these binary implementations stencils because they have holes where missing values must be inserted during code generation. We show how to construct a stencil library and describe the copy-and-patch algorithm that generates optimized binary code. We demonstrate two use cases of copy-and-patch: a compiler for a high-level C-like language intended for metaprogramming and a compiler for WebAssembly. Our high-level language compiler has negligible compilation cost: it produces code from an AST in less time than it takes to construct the AST. We have implemented an SQL database query compiler on top of this metaprogramming system and show that on TPC-H database benchmarks, copy-and-patch generates code two orders of magnitude faster than LLVM -O0 and three orders of magnitude faster than higher optimization levels. The generated code runs an order of magnitude faster than interpretation and 14% faster than LLVM -O0. Our WebAssembly compiler generates code 4.9X-6.5X faster than Liftoff, the WebAssembly baseline compiler in Google Chrome. The generated code also outperforms Liftoff's by 39%-63% on the Coremark and PolyBenchC WebAssembly benchmarks.Keywords
All Related Versions
This publication has 21 references indexed in Scilit:
- MaxineACM Transactions on Architecture and Code Optimization, 2013
- Decoupling algorithms from schedules for easy optimization of image processing pipelinesACM Transactions on Graphics, 2012
- Templates-based portable just-in-time compilerACM SIGPLAN Notices, 2003
- TempoACM Computing Surveys, 1998
- Optimizing direct threaded code by selective inliningACM SIGPLAN Notices, 1998
- VCODEACM SIGPLAN Notices, 1996
- DCGACM SIGOPS Operating Systems Review, 1994
- A linear sieve algorithm for finding prime numbersCommunications of the ACM, 1978
- Threaded codeCommunications of the ACM, 1973
- The Generation of Optimal Code for Arithmetic ExpressionsJournal of the ACM, 1970