Jump to content

Copy-and-patch

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by The Anome (talk | contribs) at 17:53, 9 January 2024 (can do ASTs as well). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Copy-and-patch compilation is a technique for just-in-time compilation that uses pattern matching to match pre-generated templates to parts of an abstract syntax tree or bytecode stream, and emit corresponding machine code that is then patched to insert memory addresses, register addresses, constants and other parameters to produce executable code. Code not matched by templates can be either be interpreted in the normal way, or code created to directly call interpreter code.

Copy-and-patch is a "quick-and-dirty" approach to compilation that is orders of magnitude faster than more rigorous techniques, it often yields code that can in many cases approach the performance of those techniques.

Copy-and-patch was first described by Fredrik Kjolstad and Haoran Xu in a 2021 paper.[1] It is used for the Python 3.13 JIT compiler.[2]

References

  1. ^ Xu, Haoran; Kjolstad, Fredrik (2021-10-15). "Copy-and-patch compilation: a fast compilation algorithm for high-level languages and bytecode". Proceedings of the ACM on Programming Languages. 5 (OOPSLA): 136:1โ€“136:30. arXiv:2011.13127. doi:10.1145/3485513.
  2. ^ "Python 3.13 gets a JIT". tonybaloney.github.io. Retrieved 2024-01-09.