]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-139877: Use PyBytesWriter in pycore_blocks_output_buffer.h (#139976)
authorEmma Smith <emma@emmatyping.dev>
Tue, 14 Oct 2025 17:03:55 +0000 (10:03 -0700)
committerGitHub <noreply@github.com>
Tue, 14 Oct 2025 17:03:55 +0000 (10:03 -0700)
commitf262297d525e87906c5e4ab28e80284189641c9e
tree65eb173245c9591b4ff765f4647324de69258ea0
parent404425575c68bef9d2f042710fc713134d04c23f
gh-139877: Use PyBytesWriter in pycore_blocks_output_buffer.h (#139976)

Previously, the _BlocksOutputBuffer code creates a list of bytes objects to handle the output data from compression libraries. This ends up being slow due to the output buffer code needing to copy each bytes element of the list into the final bytes object buffer at the end of compression.

The new PyBytesWriter API introduced in PEP 782 is an ergonomic and fast method of writing data into a buffer that will later turn into a bytes object. Benchmarks show that using the PyBytesWriter API is 10-30% faster for decompression across a variety of settings. The performance gains are greatest when the decompressor is very performant, such as for Zstandard (and likely zlib-ng). Otherwise the decompressor can bottleneck decompression and the gains are more modest, but still sizable (e.g. 10% faster for zlib)!

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Include/internal/pycore_blocks_output_buffer.h
Modules/_bz2module.c
Modules/_lzmamodule.c
Modules/_zstd/buffer.h
Modules/_zstd/compressor.c
Modules/_zstd/decompressor.c
Modules/zlibmodule.c