-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve import time of various stdlib modules #118761
Comments
Sure! |
I've opened a PR over at the
@layday were you planning on tackling these?
This seems to be solved on main, |
importlib.metadata
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This has been merged and released in version 8.4 of
I've submitted python/importlib_metadata#502 that defers zip import, and python/importlib_metadata#503 which defers |
(removing the 3.14 label since features always target the main branch) |
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Importing `pickle` is now roughly 25% faster. Importing the `re` module is no longer needed and thus `re` is no more implicitly exposed as `pickle.re`. --------- Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
@picnixz: I suggest to close this issue. |
There is also |
…ficient alternative (#128736) Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Co-authored-by: Chris Markiewicz <effigies@gmail.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
I can simplify diff --git a/Lib/pickletools.py b/Lib/pickletools.py
index d9c4fb1e63e..f39819b217a 100644
--- a/Lib/pickletools.py
+++ b/Lib/pickletools.py
@@ -13,7 +13,6 @@
import codecs
import io
import pickle
-import re
import sys
__all__ = ['dis', 'genops', 'optimize']
@@ -2225,7 +2224,7 @@ def assure_pickle_consistency(verbose=False):
copy = code2op.copy()
for name in pickle.__all__:
- if not re.match("[A-Z][A-Z0-9_]+$", name):
+ if not name.isidentifier() or not name.isupper() or name.startswith('_'):
if verbose:
print("skipping %r: it doesn't look like an opcode name" % name)
continue We would gain roughly 2 ms according to There are some modules that we can optimize:
There are other occurrences of the Note that, $ ./python -X importtime -c 'import re'
import time: self [us] | cumulative | imported package
...
import time: 496 | 3192 | site
import time: 224 | 224 | linecache
import time: 319 | 319 | types
import time: 1818 | 2136 | enum
import time: 69 | 69 | _sre
import time: 238 | 238 | re._constants
import time: 364 | 601 | re._parser
import time: 89 | 89 | re._casefix
import time: 301 | 1059 | re._compiler
import time: 243 | 243 | itertools
import time: 116 | 116 | keyword
import time: 55 | 55 | _operator
import time: 206 | 260 | operator
import time: 125 | 125 | reprlib
import time: 50 | 50 | _collections
import time: 635 | 1426 | collections
import time: 39 | 39 | _functools
import time: 526 | 1990 | functools
import time: 114 | 114 | copyreg
import time: 443 | 5741 | re We also always have import time: 1105 | 1105 | _collections_abc but we can't be more efficient there since we construct lots of classes. In conclusion, I will only open one PR for optimizing diff --git a/Lib/csv.py b/Lib/csv.py
index cd202659873..0a627ba7a51 100644
--- a/Lib/csv.py
+++ b/Lib/csv.py
@@ -63,7 +63,6 @@ class excel:
written as two quotes
"""
-import re
import types
from _csv import Error, writer, reader, register_dialect, \
unregister_dialect, get_dialect, list_dialects, \
@@ -281,6 +280,7 @@ def _guess_quote_and_delimiter(self, data, delimiters):
If there is no quotechar the delimiter can't be determined
this way.
"""
+ import re
matches = []
for restr in (r'(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?P=delim)', # ,".*?", |
Feature or enhancement
Proposal:
Following on from #109653, further improvements can be made to import times.
Links to previous discussion of this feature:
https://discuss.python.org/t/deferred-computation-evalution-for-toplevels-imports-and-dataclasses/34173
For example:
importlib.metadata
is often used for tasks that need to happen at import, e.g. to enumerate/load entry point plug-ins, so it might be worth seeing if we can cut down its own import time a bit more.importlib.metadata
importszipfile
at the top for a function that won't be called in the vast majority of cases. It also importsimportlib.abc
, which in turn importsimportlib.resources
, to subclass an ABC with a single, non-abstract method - I assume redefining the method inimportlib.metadata
would be harmless. Some other less frequently-used imports which are only accessed once or twice, such asjson
, could also be tucked away in their calling functions.Linked PRs
pprint
#122725socket
by writingsocket.errorTab
as a constant and lazy import modules #121424mimetypes
#126979pickle
#128732re
import inbase64.b16decode
for a more efficient alternative #128736secrets
#128738csv
#128858The text was updated successfully, but these errors were encountered: