Skip to content

gh-119182: Optimize PyUnicode_FromFormat() #120796

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
merged 3 commits into from
Jun 20, 2024
Merged

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Jun 20, 2024

Use strchr() and ucs1lib_find_max_char() to optimize the code path formatting sub-strings between '%' formats.

Use strchr() and ucs1lib_find_max_char() to optimize the code path
formatting sub-strings between '%' formats.
@vstinner
Copy link
Member Author

Results of #120248 (comment) benchmark:

$ python3 -m pyperf compare_to ref.json change.json --table
+----------------+--------+----------------------+
| Benchmark      | ref    | change               |
+================+========+======================+
| bench 30       | 219 ns | 202 ns: 1.08x faster |
+----------------+--------+----------------------+
| bench 100      | 262 ns | 216 ns: 1.21x faster |
+----------------+--------+----------------------+
| Geometric mean | (ref)  | 1.09x faster         |
+----------------+--------+----------------------+

Benchmark hidden because not significant (1): bench 3


if (_PyUnicodeWriter_WriteASCIIString(writer, f, len) < 0)
int is_ascii = (ucs1lib_find_max_char((Py_UCS1*)f, (Py_UCS1*)f + len) < 128);
Copy link
Member

Choose a reason for hiding this comment

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

You can run it once, for the whole format string.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right. I modified my PR to run it only once, good idea!

@vstinner vstinner enabled auto-merge (squash) June 20, 2024 18:58
@vstinner vstinner merged commit 5150795 into python:main Jun 20, 2024
34 checks passed
@vstinner vstinner deleted the optim_from_fmt branch June 20, 2024 19:06
@vstinner
Copy link
Member Author

Merged, thanks for the reviews @serhiy-storchaka.

mrahtz pushed a commit to mrahtz/cpython that referenced this pull request Jun 30, 2024
Use strchr() and ucs1lib_find_max_char() to optimize the code path
formatting sub-strings between '%' formats.
noahbkim pushed a commit to hudson-trading/cpython that referenced this pull request Jul 11, 2024
Use strchr() and ucs1lib_find_max_char() to optimize the code path
formatting sub-strings between '%' formats.
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
Use strchr() and ucs1lib_find_max_char() to optimize the code path
formatting sub-strings between '%' formats.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants