Adapting CPython for yk
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Title: Adapting CPython for yk
Earlier this year I wrote a post on yk, which can automatically add JIT compilers to C interpreters. Since then we’ve applied yk to more interpreters, fixed various bugs, and improved performance notably. Towards the end of October I showed some data where on our benchmark suite1 yklua is 2× faster than the PUC Lua interpreter it takes as a base. I was quite pleased with myself until someone brought me down to earth by asking “how do you know if this works for large interpreters?”
今年早些时候,我写了一篇关于 yk 的文章,它能够自动为 C 语言解释器添加 JIT 编译器。从那时起,我们将 yk 应用于更多的解释器,修复了各种错误,并显著提升了性能。十月底左右,我展示了一些数据,显示在我们的基准测试套件中,yklua 的运行速度是其基础 PUC Lua 解释器的两倍。我当时对自己很满意,直到有人问我“你怎么知道这对于大型解释器是否有效?”从而让我冷静了下来。
It’s a fair question: PUC Lua is at least an order of magnitude smaller2 than CPython or CRuby and who’s to say if it would work with much bigger interpreters? We had looked at CPython a year or so ago, but at that point yk was missing several obvious things that CPython would need, so we quickly moved on. A lot has changed since then, and a quick LLM experiment got far enough to suggest it would be worth looking again.
这是一个合理的问题:PUC Lua 的规模至少比 CPython 或 CRuby 小一个数量级,谁能保证它在更大的解释器上也能工作呢?我们大约一年前研究过 CPython,但当时 yk 缺少 CPython 所需的几个关键功能,所以我们很快就放弃了。从那时起,情况发生了很大变化,一次快速的 LLM 实验结果表明,重新审视它是有价值的。
Happily, having taken a deeper look, CPython not only works when adapted for yk as ykcpython3, but there are meaningful performance gains to be had — not only relative to normal CPython, but also relative to Python’s experimental JIT4. Roughly speaking, this is the result of 3-4 days’ work on ykcpython directly; there was a separate week and a half of generic yk bug fixing necessary to get to the point that I could look at ykcpython itself.
令人高兴的是,经过深入研究,CPython 不仅在适配为 ykcpython 后可以运行,而且还获得了显著的性能提升——不仅相对于普通 CPython,甚至相对于 Python 的实验性 JIT 也是如此。粗略地说,这是直接在 ykcpython 上工作 3 到 4 天的结果;在此之前,还需要花一周半的时间进行通用的 yk 错误修复,才能让我开始着手处理 ykcpython 本身。
A quick refresher: If you haven’t read the earlier yk post, here’s a very brief refresher. Given a lightly modified C interpreter6 as input, yk produces a VM with a meta-tracing JIT compiler as output7. yk doesn’t aim to produce the fastest possible JIT compiling language implementation: rather, it aims to make the language implementation you know and love somewhat faster while retaining 100% compatibility.
快速回顾:如果你没有读过之前关于 yk 的文章,这里做一个简要回顾。以一个经过轻微修改的 C 解释器作为输入,yk 会输出一个带有元追踪(meta-tracing)JIT 编译器的虚拟机。yk 的目标并不是产生最快的 JIT 编译语言实现,而是旨在让你熟悉且喜爱的语言实现变得更快,同时保持 100% 的兼容性。
Put another way a “ykified” interpreter is a fork of the existing C version of the interpreter, with (hopefully!) a handful of adjustments. As with any meta-tracing JIT compiler, yk identifies hot loops in the guest language (e.g. Lua or Python), and traces the interpreter (i.e. records what the C code is executing while it is interpreting the guest language). It then optimises those traces and converts them to machine code.
换句话说,“yk 化”的解释器是现有 C 版本解释器的一个分支,只需(希望如此!)进行少量的调整。与任何元追踪 JIT 编译器一样,yk 会识别宿主语言(如 Lua 或 Python)中的热循环,并追踪解释器(即记录 C 代码在解释宿主语言时执行的内容)。然后,它会优化这些追踪并将其转换为机器码。
yk requires interpreters to tell it where potential loops in the guest language are via the YkLocation struct. Optionally – though if you want to get good performance, definitely necessary! – interpreter authors can add optimisation hints that embed knowledge about the language or its common idioms of use such as “at this point this seemingly dynamic value almost never changes”.
yk 要求解释器通过 YkLocation 结构体告知其宿主语言中潜在循环的位置。可选地——尽管如果你想获得良好的性能,这绝对是必要的!——解释器作者可以添加优化提示,嵌入关于语言或其常用习惯用法的知识,例如“在这一点上,这个看似动态的值几乎从不改变”。
In general, working out where to put YkLocations is relatively easy, but working out where to put hints requires a much greater understanding of the interpreter, and careful measurement to understand which do and don’t help. The challenge I face when adapting an interpreter for yk is that I rarely know the interpreter well, if at all. Rather than trying to do everything at once, I have learnt to start off as simple as possible: I try and do the minimum changes that lead to some traces being recorded and compiled to machine code.
通常,确定 YkLocation 的放置位置相对容易,但确定提示的放置位置则需要对解释器有更深入的了解,并进行仔细的测量以了解哪些有效,哪些无效。我在为 yk 适配解释器时面临的挑战是,我很少能很好地了解该解释器。与其试图一次完成所有事情,我学会了从尽可能简单的地方开始:我尝试进行最小的改动,从而使一些追踪能够被记录并编译成机器码。