Skip to content

gh-129813, PEP 782: Add PyBytesWriter.overallocate - #138941

Merged
vstinner merged 2 commits into
python:mainfrom
vstinner:pybyteswriter_overallocate
Sep 15, 2025
Merged

vstinner merged 2 commits into
python:mainfrom
vstinner:pybyteswriter_overallocate

Conversation

@vstinner

@vstinner vstinner commented Sep 15, 2025 •

Copy link
Copy Markdown
Member
  • Disable overallocation in _PyBytesWriter_CreateByteArray().
  • Disable overallocation in _PyBytes_FormatEx() at the last write.
* Disable overallocation in _PyBytesWriter_CreateByteArray().
* Disable overallocation in _PyBytes_FormatEx() at the last write.
@vstinner

Copy link
Copy Markdown
Member Author

Disable overallocation in _PyBytes_FormatEx() at the last write.

Benchmark: python -m pyperf timeit -s 's=(b"a" * 254 + b"%s"); arg=b"b"*100' 's % (arg,)'.

Result: Mean +- std dev: [ref] 165 ns +- 4 ns -> [change] 147 ns +- 1 ns: 1.12x faster. It's 18 ns faster because we avoid a realloc().

@vstinner

Copy link
Copy Markdown
Member Author

@serhiy-storchaka: This change reintroduces the overallocate=0 micro-optimization in the PyBytesWriter API, but the API to disable overallocation is only available in the internal C API.

@vstinner
vstinner merged commit 4e00e25 into python:main Sep 15, 2025
@vstinner
vstinner deleted the pybyteswriter_overallocate branch September 15, 2025 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

1 participant