Repository navigation
Freeze all modules imported during startup. #89183
Copy link
Copy link
Closed
Labels
3.11only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement
Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = 'https://github.com/ericsnowcurrently'
closed_at = <Date 2021-10-28.19:52:39.815>
created_at = <Date 2021-08-26.19:23:58.261>
labels = ['interpreter-core', 'type-feature', '3.11']
title = 'Freeze all modules imported during startup.'
updated_at = <Date 2021-11-24.19:01:47.807>
user = 'https://github.com/ericsnowcurrently'bugs.python.org fields:
activity = <Date 2021-11-24.19:01:47.807>
actor = 'christian.heimes'
assignee = 'eric.snow'
closed = True
closed_date = <Date 2021-10-28.19:52:39.815>
closer = 'eric.snow'
components = ['Parser']
creation = <Date 2021-08-26.19:23:58.261>
creator = 'eric.snow'
dependencies = ['45186', '45188']
files = []
hgrepos = []
issue_num = 45020
keywords = ['patch']
message_count = 96.0
messages = ['400370', '400371', '400372', '400373', '400374', '400381', '400386', '400394', '400421', '400422', '400427', '400447', '400449', '400450', '400454', '400455', '400459', '400460', '400461', '400469', '400507', '400508', '400510', '400629', '400636', '400638', '400639', '400660', '400664', '400665', '400666', '400667', '400675', '400766', '400769', '400808', '400855', '400856', '401027', '401040', '401734', '401740', '401805', '401811', '401813', '401814', '401848', '401865', '401870', '401905', '401911', '401917', '401918', '401919', '401921', '401954', '401966', '401988', '402017', '402020', '402070', '402080', '402100', '402101', '402103', '402113', '402116', '402118', '402119', '402151', '402356', '402463', '402464', '402476', '402587', '402609', '402629', '402633', '402634', '402898', '402993', '403024', '403025', '403255', '403256', '403323', '403324', '404111', '404164', '404257', '404344', '405002', '405203', '405244', '406143', '406948']
nosy_count = 25.0
nosy_names = ['lemburg', 'gvanrossum', 'barry', 'brett.cannon', 'nascheme', 'rhettinger', 'terry.reedy', 'gregory.p.smith', 'ronaldoussoren', 'ncoghlan', 'vstinner', 'larry', 'christian.heimes', 'christian.heimes', 'christian.heimes', 'methane', 'Mark.Shannon', 'eric.snow', 'indygreg', 'lys.nikolaou', 'pablogsal', 'miss-islington', 'brandtbucher', 'BTaskaya', 'shihai1991', 'FFY00', 'santhu_reddy12']
pr_nums = ['28107', '28320', '28335', '28344', '28345', '28346', '28375', '28380', '28392', '28398', '28410', '28538', '28554', '28583', '28590', '28635', '28664', '28665', '28655', '28940', '28997', '29755', '29755', '29755']
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue45020'
versions = ['Python 3.11']Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
3.11only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement
Activity
ericsnowcurrently commentedon Aug 26, 2021
Currently we freeze the 3 main import-related modules into the python binary (along with one test module). This allows us to bootstrap the import machinery from Python modules. It also means we get better performance importing those modules.
If we freeze modules that are likely to be used during execution then we get even better startup times. I'll be putting up a PR that does so, freezing all the modules that are imported during startup. This could also be done for any stdlib modules that are commonly imported.
(also see bpo-45019 and faster-cpython/ideas#82)
ericsnowcurrently commentedon Aug 26, 2021
FYI, with my branch I'm getting a 15% improvement to startup for "./python -c pass".
ericsnowcurrently commentedon Aug 26, 2021
I'm aware of two potentially problematic consequences to this change:
For the former, I'm not sure there's a way around it. We may consider the inconvenience worth it in order to get the performance benefits.
For the latter, the obvious solution is to introduce a startup hook (e.g. on the CLI) like we've talked about doing for years. (I wasn't able to find previous discussions on that topic after a quick search.)
malemburg commentedon Aug 26, 2021
Not sure whether you are aware, but the PyRun project I'm maintaining
does that and goes beyond this by freezing almost the complete stdlib
and statically linking most C extensions into a single binary:
https://www.egenix.com/products/python/PyRun/
Startup is indeed better, but not as much as you might think.
You do save stat calls and can share resources across processes.
The big time consumer is turning marshal'ed code objects back
into Python objects, though. If that could be made faster by
e.g. using a more efficient storage format such as one which is
platform dependent, it'd be a much bigger win than the freezing
approach.
ericsnowcurrently commentedon Aug 26, 2021
That's something Guido has been exploring. :)
See: faster-cpython/ideas#84 (and others)
ericsnowcurrently commentedon Aug 26, 2021
I'm not sure why I said "obvious". Sorry about that.
gvanrossum commentedon Aug 27, 2021
I noticed nedbat un-nosied himself. Probably he didn't realize you were
calling him out because it's possible this would affect coverage.py?
89 remaining items
gvanrossum commentedon Oct 5, 2021
Whoa. os.path is not always an alias for posixpath, is it?
larryhastings commentedon Oct 6, 2021
Nope. On Windows, os.path is "ntpath".
ericsnowcurrently commentedon Oct 6, 2021
On Tue, Oct 5, 2021 at 11:31 AM Guido van Rossum <report@bugs.python.org> wrote:
Steve brought this to my attention a couple weeks ago. Bottom line:
the frozen module entry is only there for checks, not for actual
import, but should probably be removed regardless. See
https://bugs.python.org/issue45272.
ericsnowcurrently commentedon Oct 16, 2021
New changeset b9cdd0f by Eric Snow in branch 'main':
bpo-45020: Default to using frozen modules unless running from source tree. (gh-28940)
b9cdd0f
gpshead commentedon Oct 18, 2021
could changes related to this be the cause of https://bugs.python.org/issue45506 ?
out of tree builds in main usually cannot pass key tests today. they often hang or blow up with strange exceptions.
gvanrossum commentedon Oct 19, 2021
Is #73126 only for UNIX?
I built on Windows with default options (PCbuild\build.bat) and it looks like the frozen modules are used by default even though I am running in the source directory. (I put a printf() call in unmarshal_frozen_code().)
I also put a printf() in is_dev_env() and found that it returns 0 on this check:
I assume that's because the binary (in my case at least) is at PCbuild\amd64\python.exe which is not the same as my current directory (which is the repo root).
ericsnowcurrently commentedon Oct 19, 2021
On Mon, Oct 18, 2021 at 7:14 PM Guido van Rossum <report@bugs.python.org> wrote:
It wasn't meant to be. :(
I'll look into this.
ericsnowcurrently commentedon Oct 25, 2021
New changeset 6afb285 by Eric Snow in branch 'main':
bpo-45020: Add tests for the -X "frozen_modules" option. (gh-28997)
6afb285
ericsnowcurrently commentedon Oct 28, 2021
On Mon, Oct 18, 2021 at 7:14 PM Guido van Rossum <report@bugs.python.org> wrote:
FYI, I opened https://bugs.python.org/issue45651 for sorting this out.
ericsnowcurrently commentedon Oct 28, 2021
I consider this done. There is some lingering follow-up work, for which I've created a number of issues:
gvanrossum commentedon Nov 11, 2021
New changeset 1cbaa50 by Guido van Rossum in branch 'main':
bpo-45696: Deep-freeze selected modules (GH-29118)
1cbaa50
tiran commentedon Nov 24, 2021
New changeset 5c4b19e by Christian Heimes in branch 'main':
bpo-45020: Fix strict-prototypes warning (GH-29755)
5c4b19e