Optimize reference tracking and eliminate branching during returns and yields
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
There are a few optimizations we can make to returns and yields, but they are somewhat related so I'm grouping them into a single issue.
During a return, the VM needs to make the returned reference "heap safe" (convert any borrowed references to strong references) and then pop and clear the frame, which can involve quite a lot of decrefs.
During a yield, the VM needs to make the returned reference heap safe and then pop the frame, but not clear it.
We want to minimize the amount of refcounting operations that we do.
Before we can do much else, we should split RETURN_VALUE and YIELD_VALUE into micro-ops:
macro(RETURN_VALUE) = _MAKE_HEAP_SAFE + _RETURN_VALUE
macro(YIELD_VALUE) = _MAKE_HEAP_SAFE + _YIELD_VALUE
so that we can optimize the uops independently
Eliminate making heap safe if we know that reference already is
If TOS is a strong reference _MAKE_HEAP_SAFE -> _NOP
Avoid the branch in _RETURN_VALUE by splitting into _RETURN_VALUE_GEN and _RETURN_VALUE_FUNC
To avoid using up opcodes, we'll need to do this in the JIT, not the interpreter.
Track borrows/immortals to optimize clearing frames
E.g. if a frame has four local variables, a, b, c, d but we can tell that a and d are immortal, or borrowed, then instead of looping over all the variables, we could emit code just to decref b and c.
This might end up bloating the code, as we would need to inline the decrefs, but it could be quite a lot faster.
Linked PRs
- gh-144414
- gh-146320
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
先阅读 RETURN_VALUE 和 YIELD_VALUE 宏,然后在解释器和 JIT 中跟踪 _MAKE_HEAP_SAFE、_RETURN_VALUE 和 _YIELD_VALUE。确定范围之前,比较相关工作 gh-144414 和 gh-146320;完成这项工作需要分别优化 return 和 yield 路径,并减少不必要的引用计数,同时不导致行为回归。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- compilers, performance
- Issue 类型
- 重构
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100