Skip to content

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented Sep 15, 2025

  • 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
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
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
43 checks passed
@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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant