python / python/cpython

`_colorize` module is slow to import, affecting `traceback` and `logging` modules

未关闭
#144,384 27 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

3.14 3.15 performance stdlib type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

“It is a truth universally acknowledged, that a module in possession of great colors, suffers import time slowdown.

_colorize module is slow to import due to its use of dataclasses

python -Ximporttime -c "import _colorize"

Visualization in tuna

Image

Notice that significant time is also spent executing the _colorize, due to the creation of its dataclasses

❯ uvx python@3.15 -m cProfile -m _colorize | head -15
         5609 function calls (5489 primitive calls) in 0.006 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
      9/1    0.003    0.000    0.006    0.006 {built-in method builtins.exec}
        1    0.000    0.000    0.006    0.006 <string>:1(<module>)
        1    0.000    0.000    0.006    0.006 <frozen runpy>:199(run_module)
        1    0.000    0.000    0.006    0.006 <frozen runpy>:65(_run_code)
        1    0.000    0.000    0.006    0.006 _colorize.py:1(<module>)
        7    0.000    0.000    0.005    0.001 dataclasses.py:1432(wrap)
        7    0.000    0.000    0.005    0.001 dataclasses.py:986(_process_class)
        7    0.000    0.000    0.003    0.000 dataclasses.py:478(add_fns_to_class)
        4    0.000    0.000    0.001    0.000 inspect.py:3342(signature)
        4    0.000    0.000    0.001    0.000 inspect.py:3055(from_callable)

The slow import time directly affects (among others) the traceback module which in turn affects logging, which is 40% slower in 3.14 and 3.15 compared to 3.13. That is quite unfortunate since in applications that care about import time, logging module is typically hard to avoid (I originally discovered this when looking at pip's startup time)

 hyperfine -w 10 "python3.13 -c 'import logging'"  "python3.15 -c 'import logging'" 
Benchmark 1: python3.13 -c 'import logging'
  Time (mean ± σ):      21.0 ms ±   3.4 ms    [User: 14.8 ms, System: 5.9 ms]
  Range (min … max):    16.2 ms …  28.2 ms    155 runs
 
Benchmark 2: python3.15 -c 'import logging'
  Time (mean ± σ):      29.6 ms ±   2.1 ms    [User: 22.2 ms, System: 7.0 ms]
  Range (min … max):    26.4 ms …  35.1 ms    100 runs
 
Summary
  python3.13 -c 'import logging' ran
    1.40 ± 0.25 times faster than /home/hollas/.local/share/uv/python/cpython-3.15.0a5-linux-x86_64-gnu/bin/python3.15 -c 'import logging'

It's not very clear how to make this better (besides not using dataclasses). We tried to make traceback lazy in logging, but failed. #112995

Linked PRs
  • gh-144879
  • gh-149318

贡献指南

打开贡献指南

从这里开始

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

调研方向

_colorize 模块开始,使用 python -Ximporttime -c "import _colorize" 和给出的 cProfile 命令重现其导入配置。比较 logging 基准测试,然后查看关联的 PR gh-144879 和 gh-149318;完成的标准是减少影响 _colorizetracebacklogging 的导入开销。

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

评估

技术栈
python
领域
performance
Issue 类型
缺陷
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

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