Windows: avoid importing encodings._win_cp_codecs and time at startup
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 82/100
- issue の種類
- 機能追加
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- python
- 領域
- performance
調査の方向性
issue では Lib/encodings/__init__.py と Lib/zipimport.py が挙げられています。2か所のインポートと、win32_code_page_search_function および _parse_dostime での使用を確認してください。Windows で起動時に行われるインポートを比較し、指定された codec、zipimport、time、signal、sys のテストを実行してください。両方のインポートが使用箇所まで遅延され、テストのリグレッションなしに起動時の動作が改善すれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Feature or enhancement
On Windows, python -c pass imports two modules that Linux doesn't, and neither is needed on most starts:
encodings/__init__.pyimportscreate_win32_code_page_codecfromencodings._win_cp_codecsat module level (since 3.14). It is only called fromwin32_code_page_search_function, for acpNNNname with no codec module.zipimportimportstimeat module level fortime.mktimein_parse_dostime. On Windows,timemodule init runs_tzset, which callsGetTimeZoneInformation. The first call in a process costs about 1 ms here.
From -X importtime (3.14.4, self time, median of 15): _win_cp_codecs 0.5 ms, time 0.96 ms on Windows (0.07 ms on Linux).
Moving both imports into the functions that use them (patch below) made python -c pass 3.6 ms faster (38.4 -> 34.4 ms; 95% CI 3.1-4.2 ms over 300 interleaved runs, release build of 3.14.4 without PGO). Each change alone is worth about 2 ms (_win_cp_codecs 2.1, time 2.4).
test_codecs, test_zipimport, test_zipimport_support, test_time, test_signal and test_sys pass with the change (3.14.4 build).
Both imports are unchanged on main (2781ffe1f2). The change, against main:
patch
diff --git a/Lib/encodings/__init__.py b/Lib/encodings/__init__.py
index 04594ea..bbccb93 100644
--- a/Lib/encodings/__init__.py
+++ b/Lib/encodings/__init__.py
@@ -157,8 +157,6 @@ def search_function(encoding):
codecs.register(search_function)
if sys.platform == 'win32':
- from ._win_cp_codecs import create_win32_code_page_codec
-
def win32_code_page_search_function(encoding):
encoding = encoding.lower()
if not encoding.startswith('cp'):
@@ -173,6 +171,7 @@ if sys.platform == 'win32':
except (OverflowError, OSError):
return None
+ from ._win_cp_codecs import create_win32_code_page_codec
return create_win32_code_page_codec(cp)
codecs.register(win32_code_page_search_function)
diff --git a/Lib/zipimport.py b/Lib/zipimport.py
index 09a85f6..aae77df 100644
--- a/Lib/zipimport.py
+++ b/Lib/zipimport.py
@@ -16,7 +16,6 @@ import _frozen_importlib as _bootstrap # for _verbose_message
import _imp # for check_hash_based_pycs
import _io # for open
import marshal # for loads
-import time # for mktime
__all__ = ['ZipImportError', 'zipimporter']
@@ -767,6 +766,8 @@ def _compile_source(pathname, source, module):
# Convert the date/time values found in the Zip archive to a value
# that's compatible with the time stamp stored in .pyc files.
def _parse_dostime(d, t):
+ # Imported here: importing time at start-up costs a time zone lookup.
+ import time
return time.mktime((
(d >> 9) + 1980, # bits 9..15: year
(d >> 5) & 0xF, # bits 5..8: month
I used GitHub Copilot (Claude Opus 5.5 model) to trace this, build and time the prototype, and draft this issue. The measurements were run on my machine.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
- 主要言語
- Python
- スター
- 77.9k
- フォーク
- 37.1k
- 平均マージ
- 17時間 43分
- マージ済み PR(30日)
- 675
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/cpython のほかの issue
-
type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
OS-windows performance stdlib type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
build OS-unsupported OS-windows type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 60/100
メンテナーはふだん 1 日以内に返信
-
What's New in 3.12 credits the unstable C API tier to PEP 697 instead of PEP 689対応中かも @TheLouisMahdi が 4 日前に担当しました。 オープンdocs
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
python/cpython#161078 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
OS-windows type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
docs size:S
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
メンテナーはふだん 1 日以内に返信
-
documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
strawberry-graphql/strawberry#4794 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
dataforgoodfr/cetolog#21 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
btclib-org/btclib#2932 ·
メンテナーはふだん 1 日以内に返信