-
-
Notifications
You must be signed in to change notification settings - Fork 32.9k
gh-139156: Use PyBytesWriter in UTF-32 encoder #139157
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
Conversation
Replace PyBytes_FromStringAndSize() and _PyBytes_Resize() with the PyBytesWriter API.
Benchmark ASCII characters: import pyperf
runner = pyperf.Runner()
for size in (3, 100, 1000):
runner.timeit(f'{size:,} chars',
setup=f's="x"*{size}',
stmt='s.encode("utf32")') Result:
Benchmark hidden because not significant (1): 3 chars |
Benchmark UCS-4 characters: import pyperf
runner = pyperf.Runner()
for size in (3, 100, 1000):
runner.timeit(f'{size:,} UCS-4 chars',
setup=f's=chr(0x10ffff) * {size}',
stmt='s.encode("utf32")') Result:
|
The only significant difference is on "3 UCS-4 chars": 54.7 ns => 63.9 ns: 1.17x slower, +9.2 seconds. That's the cost of the PyBytesWriter API abstraction. I added a "fast path" for UCS-1 which is the most common cases: it keeps |
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.
LGTM. 👍
Merged, thanks for the review @serhiy-storchaka. |
Replace PyBytes_FromStringAndSize() and _PyBytes_Resize() with the PyBytesWriter API.