Skip to content
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

Use a buffer pool #856

Open
shayded-exe opened this issue Dec 4, 2023 · 4 comments
Open

Use a buffer pool #856

shayded-exe opened this issue Dec 4, 2023 · 4 comments

Comments

@shayded-exe
Copy link

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.

@Links2004
Copy link
Owner

Links2004 commented Dec 7, 2023

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.

@shayded-exe
Copy link
Author

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

@shayded-exe
Copy link
Author

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.

@Links2004 Links2004 reopened this Dec 15, 2023
@Links2004
Copy link
Owner

for the ws header the lib uses a static buffer.

uint8_t cWsHeader[WEBSOCKETS_MAX_HEADER_SIZE]; ///< RX WS Message buffer

but for the payload its malloc.

if(header->payloadLen > 0) {
// if text data we need one more
payload = (uint8_t *)malloc(header->payloadLen + 1);
if(!payload) {
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] to less memory to handle payload %d!\n", client->num, header->payloadLen);
clientDisconnect(client, 1011);
return;
}
readCb(client, payload, header->payloadLen, std::bind(&WebSockets::handleWebsocketPayloadCb, this, std::placeholders::_1, std::placeholders::_2, payload));

if(payload) {
free(payload);
}

you can modify this in code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants