Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-139871: Optimize small takes in bytearray.take_bytes#141741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Conversation
cmaloney commented Nov 19, 2025 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
When less than half the buffer is taken just copy that small part out rather than doing a big alloc + memmove + big shrink.
Uh oh!
There was an error while loading. Please reload this page.
vstinner left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Uh oh!
There was an error while loading. Please reload this page.
encukou commented Nov 19, 2025
Hmm, would it make sense to take this path on |
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
cmaloney commented Nov 20, 2025 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
I'd need to measure more / not sure. The realloc code does memcpy + malloc if 25% of space can be saved: Line 2676 in ca1e86f
Doing a malloc + memcpy + free instead of the memmove + malloc + memcpy + free would be good but not sure how much to tune to the particular allocator constants. My theory for usage pattern has been reading a block of bytes off a stream (network or terminal), appending (or readinto) the bytearray, then taking little chunks out of that as bytes. With that bytearray realloc code should recompact periodically effectively meaning this turns into a reasonably efficient ringbuffer.... (for that use case a "please repack but don't reduce capacity flag may be useful"). Not sure how common that usage is going to be though |
encukou commented Nov 20, 2025
That sounds like Anyway, no need to block this PR on that. Thank you! |
e265ce8 into python:mainUh oh!
There was an error while loading. Please reload this page.
bedevere-bot commented Nov 20, 2025
|
cmaloney commented Nov 20, 2025
Test failure looks independent of this change, |
…GH-141741) When less than half the buffer is taken just copy that small part out rather than doing a big alloc + memmove + big shrink.
…GH-141741) When less than half the buffer is taken just copy that small part out rather than doing a big alloc + memmove + big shrink.
When less than half the buffer is taken just copy that small part out rather than doing a big alloc + memcpy + big shrink.
cc: @vstinner, @encukou
.take_bytes([n])a zero-copy path frombytearraytobytes#139871