Windows: avoid importing encodings._win_cp_codecs and time at startup

未关闭 适合新手
#161,510 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 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 内容生成。

描述

OS-windows performance stdlib type-feature
Feature or enhancement

On Windows, python -c pass imports two modules that Linux doesn't, and neither is needed on most starts:

  • encodings/__init__.py imports create_win32_code_page_codec from encodings._win_cp_codecs at module level (since 3.14). It is only called from win32_code_page_search_function, for a cpNNN name with no codec module.
  • zipimport imports time at module level for time.mktime in _parse_dostime. On Windows, time module init runs _tzset, which calls GetTimeZoneInformation. 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

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

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

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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