Skip to content

reduce "python_3000_invalid_escape_sequence" execution time by ~60% #1293

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

Merged

Conversation

abebus
Copy link

@abebus abebus commented Jul 17, 2025

@abebus abebus mentioned this pull request Jul 17, 2025
@abebus abebus changed the title this reduces execution time from ~5700 ns to ~3400 ns this reduces function execution time by ~60% Jul 17, 2025
@abebus abebus changed the title this reduces function execution time by ~60% reduce "python_3000_invalid_escape_sequence" execution time by ~60% Jul 17, 2025
@asottile asottile force-pushed the speedups/python_3000_invalid_escape_sequence branch from a315899 to f9301e4 Compare August 2, 2025 17:42
Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asottile asottile merged commit 09bd5bd into PyCQA:main Aug 2, 2025
11 checks passed
Comment on lines -1664 to +1669
if token_type in {tokenize.STRING, FSTRING_START, TSTRING_START}:
if (
token_type == tokenize.STRING or
token_type == FSTRING_START or
token_type == TSTRING_START
):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am watching your stream VOD and I simply wanted to let you know that the reason the previous solution was very slow is that Python LOAD_CONST optimization applies only to trivial Python objects (with guaranteed no side effects). tokenize.STRING may have side effects, which prevents this optimization and makes Python construct the set each time you access it in a conditional. My typical workaround for such performance-oriented cases, without polluting the global scope with lots of sets/dicts, is to simply put them in kwargs of the function that run just once. See this bytecode: https://godbolt.org/z/qoPdYvha3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants