Windows: avoid importing encodings._win_cp_codecs and time at startup
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 82/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- python
- Lĩnh vực
- performance
Hướng nghiên cứu
Issue chỉ ra Lib/encodings/__init__.py và Lib/zipimport.py; hãy đọc hai vị trí import và cách chúng được dùng trong win32_code_page_search_function và _parse_dostime. So sánh các import khi khởi động trên Windows và chạy các bài kiểm thử codec, zipimport, time, signal và sys đã nêu. Hoàn tất khi cả hai import được hoãn đến vị trí sử dụng và hành vi khởi động được cải thiện mà không gây lỗi hồi quy trong các bài kiểm thử.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- Python
- Star
- 77.9k
- Fork
- 37.1k
- Merge trung bình
- 17 giờ 43 phút
- Pull request đã merge (30 ngày)
- 675
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của python/cpython
-
type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
OS-windows performance stdlib type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
build OS-unsupported OS-windows type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 60/100
Maintainer thường phản hồi trong vòng 1 ngày
-
What's New in 3.12 credits the unstable C API tier to PEP 697 instead of PEP 689Có thể đã có người làm @TheLouisMahdi đã nhận 4 ngày trước. Đang mởdocs
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
python/cpython#161078 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
OS-windows type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của python/cpython
Issue tương tự
-
docs size:S
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
strawberry-graphql/strawberry#4794 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
dataforgoodfr/cetolog#21 ·
-
security_citations_test fails against ecc 2026.11.6: SECURITY.md quotes ecc v2026.11.2's linesĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
btclib-org/btclib#2932 ·
Maintainer thường phản hồi trong vòng 1 ngày