fix: reuse BytesIO buffer to avoid reallocation in StreamBuffer#4507
fix: reuse BytesIO buffer to avoid reallocation in StreamBuffer#4507darshankparmar wants to merge 1 commit intolivekit:mainfrom
Conversation
| self._buffer.seek(0) | ||
| self._buffer.truncate() | ||
| self._buffer.write(remaining) | ||
| self._buffer.seek(0) |
There was a problem hiding this comment.
I don't think this works.
would future bytes be written at 0? overwriting remaining? reading and writing are happening at the same position, which isn't what we want
There was a problem hiding this comment.
I don't think this works.
would future bytes be written at 0? overwriting remaining? reading and writing are happening at the same position, which isn't what we want
Thanks for the review. After a deeper look, you’re right.
While the truncate-based approach reduces allocations, it relies on implicit BytesIO cursor behavior and isn’t robust enough for a concurrency primitive.
Alternative designs (e.g. ring buffers) would break PyAV’s file-like contract or require a larger redesign.
I’ll revert this change and keep the existing behavior for correctness.
This PR reduces memory churn in
StreamBuffer.read()by reusing the existingBytesIObuffer instead of reallocating a new one after each read.