arduinoWebSockets icon indicating copy to clipboard operation
arduinoWebSockets copied to clipboard

Use a buffer pool

Open shayded-exe opened this issue 1 year ago • 4 comments

I'm consistently sending large messages of the same size. It would be much more efficient to be able to reuse the payload buffer instead of a new one being malloc'd each time.

shayded-exe avatar Dec 04 '23 03:12 shayded-exe

Hi, somthing like this is possible via the headerToPayload parameter, with this you can skip all the internal copys by providing your data with the required free and unused space (at the beginning of the buffer) to add the websocket header directly to you buffer.

the WEBSOCKETS_MAX_HEADER_SIZE will tell you the size you need to reserver.

Links2004 avatar Dec 07 '23 16:12 Links2004

Thank you, I didn't realize this was possible!

shayded-exe avatar Dec 07 '23 23:12 shayded-exe

Sorry, I should've clarified, I'm receiving messages, not sending. Is there a way to do this on the receive side? I didn't see one in the code.

shayded-exe avatar Dec 15 '23 05:12 shayded-exe

for the ws header the lib uses a static buffer. https://github.com/Links2004/arduinoWebSockets/blob/30d5e136665a52880f641ddd7245b3ba05ecd32b/src/WebSockets.h#L327

but for the payload its malloc.

https://github.com/Links2004/arduinoWebSockets/blob/30d5e136665a52880f641ddd7245b3ba05ecd32b/src/WebSockets.cpp#L453-L462 https://github.com/Links2004/arduinoWebSockets/blob/30d5e136665a52880f641ddd7245b3ba05ecd32b/src/WebSockets.cpp#L522-L524

you can modify this in code.

Links2004 avatar Dec 15 '23 13:12 Links2004