Copy-and-Patch: a Copy-and-Patch Tutorial
Posted3 months agoActive3 months ago
transactional.blogTechstory
calmpositive
Debate
20/100
Compiler DesignJit CompilationCode Generation
Key topics
Compiler Design
Jit Compilation
Code Generation
The 'Copy-and-Patch' tutorial presents a technique for fast compilation, sparking discussion on its applications and related projects, such as Cranelift and CPython JIT compiler.
Snapshot generated from the HN discussion
Discussion Activity
Light discussionFirst comment
40m
Peak period
4
8-10h
Avg / period
2.4
Comment distribution19 data points
Loading chart...
Based on 19 loaded comments
Key moments
- 01Story posted
Oct 14, 2025 at 1:14 AM EDT
3 months ago
Step 01 - 02First comment
Oct 14, 2025 at 1:54 AM EDT
40m after posting
Step 02 - 03Peak activity
4 comments in 8-10h
Hottest window of the conversation
Step 03 - 04Latest activity
Oct 14, 2025 at 10:49 PM EDT
3 months ago
Step 04
Generating AI Summary...
Analyzing up to 500 comments to identify key contributors and discussion patterns
ID: 45576502Type: storyLast synced: 11/20/2025, 7:40:50 PM
Want the full context?
Jump to the original sources
Read the primary article or dive into the live Hacker News thread when you're ready.
https://cranelift.dev/
Closest thing in (relatively) recent news that uses copy-and-patch I can think of is CPython's new JIT.
[0]: https://github.com/bytecodealliance/rfcs/pull/27
I wonder if there's a place for copy-and-patch within Cranelift at some level, maybe for specific sequences or operations? I had a similar experience trying to streamline some code generation tasks and found that even small optimizations could lead to surprisingly big performance gains.
I think it's cool how different teams tackle the same challenges from different angles—like how CPython's JIT works, for instance. It really makes you appreciate the depth of creativity in the community. Do you think there are other JITs out there that are using these techniques in ways we haven’t seen yet? Or maybe there are trade-offs between speed and optimization that some projects have to weigh heavier than others?
Copy-and-patch is a technique for reducing the amount of effort it takes to write a JIT by leaning on an existing AOT compiler's code generator. Instead of generating machine code yourself, you can get LLVM (or another compiler) to generate a small snippet of code for each operation in your internal IR. Then codegen is simply a matter of copying the precompiled snippet and patching up the references.
The more resources are poured into a JIT, the less it is likely to use copy-and-patch. You get more control/flexibility doing codegen yourself.
But see also Deegen for a pretty cool example of trying to push this approach as far as possible: https://aha.stanford.edu/deegen-meta-compiler-approach-high-...
From a master thesis: https://www.itspy.cz/wp-content/uploads/2025/09/it_spy_2025_...
This is, but only for someone who wants to do JIT work without writing assembly code, but can read assembly code back into C (or can automate that part).
Instead of doing all manual register allocations in the JIT, you get to fill in the blanks with the actual inputs after a more (maybe) diligent compiler has allocated the registers, pushed them and all that.
There's a similar set of implementation techniques in Apache Impala, where the JIT only invokes the library functions when generating JIT code, instead of writing inline JIT operations, so that they can rely on shorter compile times for the JIT and deeper optimization passes for the called functions.
I think WASM, but could be for a custom byte code? and more importantly, for a set of host-native functions (like I make some rust functions that somehow exploit this idea?)
https://transactional.blog/copy-and-patch/
(key terms: abus[e|ing]: 4, force: 3, trick: 1, chance: 1)
There are whole classes of problems that can be more easily solved with SMC. That's part of what got me into FPGAs back in the 90s, before I abandoned them due to their lack of exponential growth and proprietary placement and routing tools.
This could have implications for faster in-app scripting like in games. Also for building more powerful shaders. I wonder if there are analogs of the article's mprotect(ret, 256, PROT_READ | PROT_EXEC) calls for GPUs.
Copy-and-Patch: Fast compilation for high-level languages and bytecode (2020) https://news.ycombinator.com/item?id=40553448 - June 2024 (51 comments)
A copy-and-patch JIT compiler for CPython - https://news.ycombinator.com/item?id=38769874 - Dec 2023 (68 comments)
Copy-and-Patch: Fast JIT Compilation for SQL, WebAssembly, and Others - https://news.ycombinator.com/item?id=28547057 - Sept 2021 (7 comments)