-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
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
Conversation
When less than half the buffer is taken just copy that small part out rather than doing a big alloc + memmove + big shrink.
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
|
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 |
|
That sounds like Anyway, no need to block this PR on that. Thank you! |
|
|
Test failure looks independent of this change, |
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