Copy-and-patch compilation: a fast compilation algorithm for high-level languages and bytecode
Haoran Xu, Fredrik Kjolstad
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.9×-6.5× 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.
CCS Concepts: • Software and its engineering → Just-in-time compilers; Domain specific languages.
Ask about this paper
Your agent reads all of it.
Lune indexed this paper to the last equation, along with the top-tier papers that cite it. Ask a question and the answer quotes them.
Cited by top-tier papers5
- AlloyStack: A Library Operating System for Serverless Workflow ApplicationsJianing You, Kang Chen, Laiping Zhao, Yiming Li et al.EuroSys 2025 · 7 citations
- Compilation of Modular and General Sparse WorkspacesGenghan Zhang, Olivia Hsu, Fredrik KjolstadPLDI 2024 · 6 citations
- Sharing State Between Prompts and ProgramsEllie Y. Cheng, Logan Weber, Tian Jin, Michael CarbinICLR 2026 · 3 citations
- Deegen: A JIT-Capable VM Generator for Dynamic LanguagesHaoran Xu, Fredrik KjolstadOOPSLA 2026 · 2 citations
- CLIR: Liveness-Driven and Structure-Aware Fuzzing for the Cranelift CompilerShangtong Cao, Tianlei Song, Qiuping Yi, Tianyu Chen et al.ISSTA 2026
Related papers
- A fast in-place interpreter for WebAssemblyBen L. TitzerOOPSLA 2022 · 44 citations
- Adaptive Code Generation for Data-Intensive AnalyticsWangda Zhang, Junyoung Kim, Kenneth A. Ross, Eric Sedlar et al.VLDB 2021 · 12 citations
- Stencil-Lifting: Hierarchical Recursive Lifting System for Extracting Summary of Stencil Kernel in Legacy CodesMingyi Li, Junmin Xiao, Siyan Chen, Hui Ma et al.OOPSLA 2025
- An Empirical Study of Bugs in WebAssembly CompilersAlan Romano, Xinyue Liu, Yonghwi Kwon, Weihang WangASE 2021 · 43 citations
- When Function Inlining Meets WebAssembly: Counterintuitive Impacts on Runtime PerformanceAlan Romano, Weihang WangFSE 2023 · 7 citations
