python / python/cpython

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

オープン
#144,384 コメント 27 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

_colorize モジュールから始め、python -Ximporttime -c "import _colorize" と、示されている cProfile コマンドを使ってインポートプロファイルを再現します。logging のベンチマークを比較し、その後、関連付けられた PR gh-144879 と gh-149318 を確認します。完了の条件は、_colorizetracebacklogging に影響するインポートオーバーヘッドを削減することです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
performance
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。