FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-158585: Don't overallocate at first PyBytesWriter_Resize() by vstinner · Pull Request #158616 · python/cpython · GitHub

Repository navigation

gh-158585: Don't overallocate at first PyBytesWriter_Resize() - #158616

Closed
vstinner wants to merge 1 commit into
python:mainfrom
vstinner:bytes_writer_resize
Closed

vstinner wants to merge 1 commit into
python:mainfrom
vstinner:bytes_writer_resize

Conversation

vstinner commented Oct 2, 2026 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

PyBytesWriter_Resize() and PyBytesWriter_Grow() no longer overallocate when the first bytes/bytearray object is allocated. Only overallocate bytes objects at next PyBytesWriter_Resize() and PyBytesWriter_Grow() calls.

PyBytesWriter_Resize() and PyBytesWriter_Grow() no longer
overallocate when the first bytes/bytearray object is allocated.
Only overallocate bytes objects at next PyBytesWriter_Resize() and
PyBytesWriter_Grow() calls.

vstinner commented Oct 2, 2026

Copy link
Copy Markdown
Member Author

See PR gh-158615 "io.BufferedReader.readline()" change. Combining the two PRs make io.BufferedReader.readline() up to 1.13x faster (but 1.05x in the corner cases of lines 10x longer than the io.BufferedReader buffer size).

vstinner commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

I ran again benchmarks on top of the memchr() optimization:

Benchmark 1 results:

Benchmark main change
Lib/struct.py 2.82 us 3.16 us: 1.12x slower
Lib/io.py 19.9 us 20.4 us: 1.02x slower
Lib/colorsys.py 20.8 us 21.3 us: 1.03x slower
short lines 43.8 us 42.5 us: 1.03x faster
medium lines 114 us 126 us: 1.11x slower
long lines 1.11 ms 1.07 ms: 1.03x faster
Geometric mean (ref) 1.02x slower

Benchmark hidden because not significant (2): Modules/_io/bufferedio.c, Lib/typing.py

Benchmark 2 result:

Mean +- std dev: [main_lines] 268 us +- 5 us -> [change_lines] 306 us +- 24 us: 1.14x slower

So this optimization actually makes readline() slower in most benchmarks. I close this PR, it was a bad idea :-) Overallocation is efficient and it's good to use it.

vstinner closed this Oct 7, 2026
vstinner deleted the bytes_writer_resize branch October 7, 2026 15:24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode 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


Back | FazBrowse Home | New Git URL