gh-137855: Improve import time of dataclasses by lazy importing re and copy modules#148379
Open
danielhollas wants to merge 7 commits intopython:mainfrom
Open
gh-137855: Improve import time of dataclasses by lazy importing re and copy modules#148379danielhollas wants to merge 7 commits intopython:mainfrom
re and copy modules#148379danielhollas wants to merge 7 commits intopython:mainfrom
Conversation
hugovk
reviewed
Apr 11, 2026
Lib/dataclasses.py
Outdated
Comment on lines
809
to
813
| global _MODULE_IDENTIFIER_RE | ||
| if _MODULE_IDENTIFIER_RE is None: | ||
| _MODULE_IDENTIFIER_RE = re.compile(r'^(?:\s*(\w+)\s*\.)?\s*(\w+)') | ||
|
|
||
| match = _MODULE_IDENTIFIER_RE.match(annotation) |
Member
There was a problem hiding this comment.
Oh look, the regex starts with a ^ anchor, but then uses match, which is now better expressed with its prefixmatch alias:
https://docs.python.org/3.15/library/re.html#prefixmatch-vs-match
Let's either use search with ^, or prefixmatch without ^.
And a micro-optimisation for below, match[x] (introduced in 3.6) is a bit faster than match.group(x).
Contributor
Author
There was a problem hiding this comment.
Cool! I went with prefixmatch
Member
There was a problem hiding this comment.
There's also a match.group(2) on line 826.
Contributor
Author
There was a problem hiding this comment.
Ah, good catch, sorry about that
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Contributor
Author
|
The failing Ubuntu build is a transient issue during dependency installation that will hopefully go away when the job is restarted. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Split from #144387 as requested by @hugovk (tagging for review :-) This is a relatively minor win, the bigger win will come from lazy importing inspect in #144387
See below for tuna-visualized outputs from
./python -X importtime -c "import dataclasses"(the absolute import numbers are not representative as I was running a debug python build)
Before
After