Windows: avoid importing encodings._win_cp_codecs and time at startup
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 新手友好度
- 82/100
- Issue 類型
- 功能
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- python
- 領域
- performance
研究方向
issue 指出 Lib/encodings/__init__.py 和 Lib/zipimport.py;閱讀這兩個匯入位置,以及它們在 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 分鐘
- 30 天內合併 PR
- 675
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 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 天內回覆