[WIP] gh-129813: Add PyBytesWriter C API by vstinner · Pull Request #129814 · python/cpython (original) (raw)

This change has no impact on performance, even if the new public API allocates memory on the heap, instead of allocating on the stack. It uses a freelist to optimize PyBytesWriter_Create().

Example of microbenchmark on 3 functions:

import pyperf import binascii

runner = pyperf.Runner() runner.bench_func('from list 100', bytes, list(b'x' * 100)) runner.bench_func('from list 1,000', bytes, list(b'x' * 1_000))

runner.bench_func('from hex 100', bytes.fromhex, bytes(range(100)).hex()) runner.bench_func('from hex 1,000', bytes.fromhex, (b'x' * 1_000).hex())

runner.bench_func('b2a_uu', binascii.b2a_uu, b'x' * 45)

Result:

Benchmark ref public
from list 100 672 ns 647 ns: 1.04x faster
from list 1,000 6.22 us 6.12 us: 1.02x faster
from hex 100 143 ns 145 ns: 1.02x slower
from hex 1,000 1.02 us 1.03 us: 1.00x slower
Geometric mean (ref) 1.01x faster

Benchmark hidden because not significant (1): b2a_uu