Title: gh-130167: Improve speed of `inspect.formatannotation` by replacing `re` by donbarbos · Pull Request #130242 · python/cpython · GitHub
Open Graph Title: gh-130167: Improve speed of `inspect.formatannotation` by replacing `re` by donbarbos · Pull Request #130242 · python/cpython
X Title: gh-130167: Improve speed of `inspect.formatannotation` by replacing `re` by donbarbos · Pull Request #130242 · python/cpython
Description: timeit benchmark with my script: inspect_bench.py: import re import timeit from typing import List, Dict, Any, Union def old_format_annotation(annotation): if getattr(annotation, "__module__", None) == "typing": def repl(match): text = match.group() return text.removeprefix("typing.") return re.sub(r"[\w\.]+", repl, repr(annotation)) return repr(annotation) def new_format_annotation(annotation): if getattr(annotation, "__module__", None) == "typing": return (repr(annotation) .replace(".typing.", "@TYPING@") .replace("typing.", "") .replace("@TYPING@", ".typing.")) return repr(annotation) old_time = timeit.timeit(lambda: old_format_annotation(Union[List[str], Dict[str, Any]]), number=100_000) new_time = timeit.timeit(lambda: new_format_annotation(Union[List[str], Dict[str, Any]]), number=100_000) print(f"Old version (re.sub): {old_time:.6f}s") print(f"New version (.replace()): {new_time:.6f}s") print(f"Difference: {old_time / new_time:.2f}x") Result: 2.0s -> 1.18s = x1.74 as fast $ ./python -B inspect_bench.py Old version (re.sub): 2.043900s New version (.replace()): 1.175867s Difference: 1.74x ✔️ This was also the only use of re in module so i got rid of the import Issue: gh-130167
Open Graph Description: timeit benchmark with my script: inspect_bench.py: import re import timeit from typing import List, Dict, Any, Union def old_format_annotation(annotation): if getattr(annotation, "__modul...
X Description: timeit benchmark with my script: inspect_bench.py: import re import timeit from typing import List, Dict, Any, Union def old_format_annotation(annotation): if getattr(annotation, "__m...
Opengraph URL: https://github.com/python/cpython/pull/130242
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/commits/:range(.:format) |
| route-controller | pull_requests |
| route-action | commits |
| fetch-nonce | v2:51c1ad64-cf86-5154-7ce7-ffa7434a4b6f |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | AF70:D4DD7:780D6E8:A3915D0:69961175 |
| html-safe-nonce | 0447b3ca2edc2963ebccfd06ab9e523129c3ee070fe4de55aa7d83009d3a5f80 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRjcwOkQ0REQ3Ojc4MEQ2RTg6QTM5MTVEMDo2OTk2MTE3NSIsInZpc2l0b3JfaWQiOiI4MTcyMTE3NDQ3OTEyMzI5NTg5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 04131547cb2b0c1f57902c1279929a0497f765af671ebd846871551ad523386f |
| hovercard-subject-tag | pull_request:2340808632 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/130242/commits/0ecf16f063afc6760cc625d7270f4381d72248c4 |
| twitter:image | https://avatars.githubusercontent.com/u/47272787?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/47272787?s=400&v=4 |
| og:image:alt | timeit benchmark with my script: inspect_bench.py: import re import timeit from typing import List, Dict, Any, Union def old_format_annotation(annotation): if getattr(annotation, "__modul... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7490a2fd98baca866acf503b81aa249491b42e5b4bab7f7a2801be81175c8845 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 48a904441630875f5985de713035046dc65cc94c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width