I have a network protocol that sends messages frequently and I am processing them on a slow device, with even slower disk IO, using tokio
. I want to log each received message to disk, compressed. Writing received messages to disk from the same task that processes them and generates replies sometimes spikes my latency for replies, which I have to avoid.
My idea was to read the data into a BytesMut
, split()
and freeze()
the BytesMut
to get a Bytes
, send that to another thread for compression and logging, and re-use the original buffer. Unfortunately, it appears that due to the fact that messages are arriving very frequently, there are always a few Bytes
handles still alive, which means the original BytesMut
can never re-use the storage and keeps re-allocating. That seems a bit unnecessary, and I wondered if I couldn't work around that using the following design:
I have two BytesMut
, an active one and a passive. I read into the active one, splitting Bytes
from it as I do, until its capacity is exhausted. Before re-allocating I check if the passive BytesMut
has any outstanding handles to it, and if not, I switch the two BytesMut
s to read into the one that can reclaim its buffer. That seems to work quite well (but of course means I am using 2x the memory), and now I'm wondering if there are any better ideas how to handle such a situation?