Uh oh!
There was an error while loading. Please reload this page.
gh-139871: Optimize small takes in bytearray.take_bytes - #141741
Conversation
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.
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>
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! |
Uh 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