Skip to content

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented Sep 22, 2025

Remove base64SetO and base64WhiteSpace parameters.

Remove base64SetO and base64WhiteSpace parameters.
@vstinner
Copy link
Member Author

Remove base64SetO and base64WhiteSpace parameters.

These parameters could be set with the old PyUnicode_EncodeUTF7() C API, but this function has been removed:

PyObject *
PyUnicode_EncodeUTF7(const Py_UNICODE *s,
                     Py_ssize_t size,
                     int base64SetO,
                     int base64WhiteSpace,
                     const char *errors)

@vstinner
Copy link
Member Author

Benchmark:

import pyperf
runner = pyperf.Runner()
sizes = (3, 100, 1_000, 10_000)
for size in sizes:
    runner.timeit(f'{size:,} ASCII chars',
        setup=f's="x"*{size}',
        stmt='s.encode("utf7")')

Results:

Benchmark ref change
3 ASCII chars 483 ns 473 ns: 1.02x faster
100 ASCII chars 837 ns 772 ns: 1.08x faster
1,000 ASCII chars 2.93 us 2.48 us: 1.18x faster
10,000 ASCII chars 23.2 us 18.6 us: 1.24x faster
Geometric mean (ref) 1.13x faster

Note: there is no performance impact on UCS-2 and UCS-4 characters.

@vstinner
Copy link
Member Author

cc @serhiy-storchaka

@vstinner vstinner merged commit 8d83b7d into python:main Sep 24, 2025
47 checks passed
@vstinner vstinner deleted the utf7_encode_direct branch September 24, 2025 15:57
@vstinner
Copy link
Member Author

Benchmark:

import pyperf
runner = pyperf.Runner()
sizes = (1, 100, 10_000)
for size in sizes:
    runner.timeit(f'{size:,} ASCII chars',
        setup=f's="x"*{size}',
        stmt='s.encode("utf7")')

Results comparing the 3.14 branch to the main branch:

Benchmark 314 main
1 ASCII chars 496 ns 438 ns: 1.13x faster
100 ASCII chars 858 ns 812 ns: 1.06x faster
10,000 ASCII chars 22.8 us 18.4 us: 1.24x faster
Geometric mean (ref) 1.14x faster

So even with the PyBytesWriter API (PEP 782), the UTF-7 encoder is now 1.14x faster overall on ASCII characters.

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