Skip to content

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented Sep 22, 2025

Replace PyBytes_FromStringAndSize() and _PyBytes_Resize() with the PyBytesWriter API.

Replace PyBytes_FromStringAndSize() and _PyBytes_Resize() with the
PyBytesWriter API.
@vstinner
Copy link
Member Author

vstinner commented Sep 22, 2025

Benchmark:

import pyperf
runner = pyperf.Runner()
for size in (3, 100, 1000):
    runner.timeit(f'{size:,} ASCII chars',
        setup=f's="x"*{size}',
        stmt='s.encode("utf16")')
for size in (3, 100, 1000):
    runner.timeit(f'{size:,} UCS-4 chars',
        setup=f's=chr(0x10ffff) * {size}',
        stmt='s.encode("utf16")')

Results with CPU isolation and python -m pyperf system tune:

Benchmark ref pep782
3 ASCII chars 91.2 ns 93.0 ns: 1.02x slower
3 UCS-4 chars 101 ns 116 ns: 1.14x slower
100 UCS-4 chars 402 ns 416 ns: 1.04x slower
1,000 UCS-4 chars 2.83 us 3.03 us: 1.07x slower
Geometric mean (ref) 1.04x slower

Benchmark hidden because not significant (2): 100 ASCII chars, 1,000 ASCII chars

UPDATE: Results recomputed with python -m pyperf system tune.

@vstinner vstinner merged commit c497694 into python:main Sep 22, 2025
47 checks passed
@vstinner vstinner deleted the utf16 branch September 22, 2025 21:36
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.

1 participant