python / python/cpython

Reduce overhead of PyErr_CheckSignals

未关闭
#157,746 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

interpreter-core performance type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Feature or enhancement

Proposal:

PyErr_CheckSignals() is the API long-running C loops use to stay interruptible. In the common case (no signal arrived) the call currently does, in order:

  1. _PyRunRemoteDebugger() (since gh-131591, 3.14): a call that fetches the interpreter config via _PyInterpreterState_GetConfig() before looking at the pending flag;
  2. _Py_ThreadCanHandleSignals(): _Py_IsMainThread() calls PyThread_get_thread_ident() and therefore pthread_self(), plus _Py_IsMainInterpreter();
  3. only then _PyErr_CheckSignalsTstate(), whose first line is the cheap is_tripped test that returns immediately.

That is about 100 instructions per call.

Proposal

Test the cheap flags first, without changing what is done when they are set:

  • _PyErr_CheckSignalsTstate() does the main-thread check itself, after its existing is_tripped test, so PyErr_CheckSignals() and the eval loop just call it; the handler-running body moves to an out-of-line helper so the early return stays cheap;
  • _PyRunRemoteDebugger() tests debugger_pending_call before fetching the config.
Results

Non-PGO build, pyperf:

Benchmark main branch
str(12345) 68.6 ns 56.8 ns: 1.21x faster
repr(list(range(100))) 3.06 us 2.36 us: 1.29x faster
', '.join(map(str, range(100))) 6.90 us 5.67 us: 1.22x faster
'%s=%r' % (k, v) 192 ns 177 ns: 1.08x faster
print(*range(100), file=f) 15.6 us 13.5 us: 1.16x faster
csv.writer: 100 rows of 10 ints 72.5 us 60.1 us: 1.21x faster
repr(dataclass with 5 fields) 805 ns 720 ns: 1.12x faster
a * b (10 x 10 digits) 159 ns 102 ns: 1.56x faster
a // c (10 // 2 digits) 151 ns 121 ns: 1.24x faster

Notes: the gain for the arithmetic cases are not too important, they can be handled directly as well. See https://github.com/python/cpython/issues/157742.

Has this already been discussed elsewhere?

Related: gh-131591, gh-133465

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-157748

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先阅读 PyErr_CheckSignals()、_PyErr_CheckSignalsTstate() 和 _PyRunRemoteDebugger(),重点关注所述的低成本 flag 检查和信号处理顺序。将提议的行为和基准测试与链接的 PR gh-157748 进行比较;当无信号路径的成本更低,且在设置 flag 时不改变行为时,该 issue 即完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
c, python
领域
performance
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。