Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
gh-130167: Improve speed ofinspect.formatannotation
by replacingre
#130242
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Conversation
inspect.formatannotation
by replacingre
inspect.formatannotation
by replacingre
Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I'm less sure this is worth it, the replacement isn't clearly better maintenence-wise.
What do the benchmarks look like if you extractrepl
to a module-level_formatannotation_repl
and use.sub()
on a pre-compiled pattern?
A
donBarbos commentedMay 1, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
sorry, I forgot how python was compiled then :( but I'm pretty sure I compiled it with gcc last time so I reran the test with the following config args Old version (re.sub): 0.982520sNew version (.replace()): 0.701049sDifference: 1.40x# sometimes it's 1.35x and if we rewrite old version of benchmark as you suggested: _pattern=re.compile(r"[\w\.]+")def_repl(match):text=match.group()returntext.removeprefix("typing.")defold_format_annotation(annotation):ifgetattr(annotation,"__module__",None)=="typing":returnre.sub(_pattern,_repl,repr(annotation))returnrepr(annotation) we will get a bigger difference: Old version (re.sub): 1.027774sNew version (.replace()): 0.698902sDifference: 1.47x# the result can drop to 1.40 P.S.: another reason i like the PR version is because we get rid of the |
Uh oh!
There was an error while loading.Please reload this page.
timeit
benchmark with my script:inspect_bench.py
:Result: 2.0s -> 1.18s = x1.74 as fast
re
uses #130167