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.
Declare variables earlier
Add _PyBytesWriter_GetSize() and _PyBytesWriter_GetData() static
inline functions.
@vstinner
Copy link
Member Author

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("iso8859-15")')
for size in (3, 100, 1000):
    runner.timeit(f'{size:,} UCS-2 chars',
        setup=f's="€"*{size}',
        stmt='s.encode("iso8859-15")')

Results:

Benchmark ref pep782
3 ASCII chars 654 ns 674 ns: 1.03x slower
100 ASCII chars 1.11 us 1.13 us: 1.02x slower
1,000 ASCII chars 5.32 us 5.81 us: 1.09x slower
3 UCS-2 chars 659 ns 678 ns: 1.03x slower
100 UCS-2 chars 1.16 us 1.19 us: 1.02x slower
1,000 UCS-2 chars 5.81 us 6.23 us: 1.07x slower
Geometric mean (ref) 1.04x slower

@vstinner
Copy link
Member Author

Results on PGO+LTO build with CPU isolation:

Benchmark ref pep782
3 ASCII chars 563 ns 583 ns: 1.04x slower
100 ASCII chars 1.27 us 1.35 us: 1.06x slower
1,000 ASCII chars 7.87 us 8.77 us: 1.12x slower
3 UCS-2 chars 563 ns 585 ns: 1.04x slower
100 UCS-2 chars 1.33 us 1.38 us: 1.03x slower
1,000 UCS-2 chars 8.31 us 9.18 us: 1.11x slower
Geometric mean (ref) 1.06x slower

@vstinner vstinner merged commit c9a79a0 into python:main Sep 24, 2025
77 of 79 checks passed
@vstinner vstinner deleted the charmap branch September 24, 2025 14:15
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