1 year ago
#384261
Semisonic
Is there a way to grow, slice and shrink byte buffers without reallocation in Python?
I'm using asyncio.StreamReader
to receive data from a remote source. This class has a read() method which returns a bytes
object.
Right now I use another bytes
object to accumulate data I receive from the reader:
buffer_size = 128
buffer = b""
new_data = await stream_reader.read(buffer_size)
buffer += new_data
Then eventually I extract a payload message from the buffer, process it and get rid of the raw data from which the message was generated:
buffer = buffer[payload_length:]
There are multiple reallocations here, as bytes
objects are immutable so every time I append something to the buffer the whole buffer is reallocated.
I understand that there's bytearray
, but it's unclear which operations on it are mutating. Would
ba = bytearray()
ba += new_data
avoid any relocations?
Also, can I shrink a bytearray
object while retaining its underlying memory buffer, similar to std::vector.clear
in C++?
In general, what's the efficient way of manipulating byte buffers which minimises reallocation?
python
arrays
memory-management
0 Answers
Your Answer