Windows build importlib performance regression

未关闭
#99,858 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
python

调研方向

首先,使用提供的 import_models.py 和 some_class.py 配合 -X importtime,复现 Windows 10 上的计时差异;然后比较执行 python -m compileall . 前后的运行结果,以及使用 -B 时的运行结果。检查加载模块时涉及的 importlib 和 Windows 特定导入路径。完成的标准是确定缓存导入变慢的原因,并通过经过验证的对比记录或修复该回归。

由索引模型根据 Issue 内容生成。

描述

topic-importlib type-bug

Bug report

I've a file import_models.py:

import time
from ctypes import c_longdouble
from multiprocessing import Value


def import_models_func(_, import_value, exec_value):
    init = time.process_time()
    import some_class
    import_value.value = time.process_time() - init
    exec_value.value = 0


if __name__ == '__main__':
    import_models_func(0, Value(c_longdouble, lock=False), Value(c_longdouble, lock=False))

The some_class.py is:

import time


class SomeClass:
    __slots__ = ('a', 'b', 'c')

    def __init__(self):
        self.a = time.time()
        self.b = self.a / 100
        self.c = self.b ** 2

    B = 2
    for _ in range(10):
        B **= 2

    @property
    def a_val(self):
        return self.a

    @a_val.getter
    def a_val(self):
        return self.a

    @property
    def b_val(self):
        return self.b

    @b_val.getter
    def b_val(self):
        return self.b

    @property
    def c_val(self):
        return self.c

    @c_val.getter
    def c_val(self):
        return self.c

The directory containing both files has no __pycache__. I run python -X importtime import_models.py and receive:

import time:       521 |        521 | some_class

Then I run python -m compileall . and re-run the command above, the output is:

import time:       548 |        548 | some_class

I've tried the sampling with multiprocessing governor, which is run with -B. The governor runs this scenario for 100 times, both mean and median statistics values of the timing measured mostly provide with the same results: the timing without __pycache__ is less than when the source files are pre-compiled. Which is a non-sense keeping in mind what the pre-compiled byte-code cache is built for.

I've searched for Windows-related fixes through the versions 3.9 to 3.12 and found nothing about this regression.

Linux build in a native (non-WSL, non-emulated) environment does not have such an issue (the bytecode-compiled version gets imported faster, up to 10 times, than a raw non-compiled source)

Your environment

  • CPython versions tested on: Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] on win32
  • Operating system and architecture: Windows 10 Enterprise 21H2 19044.2251
主要语言
Python
星标
77.2k
派生
36k
平均合并
1 天 9 小时
30 天内合并 PR
558

贡献指南

打开贡献指南

从这里开始

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

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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