Skip to content

Commit 4871631

Browse files
committed
fix(lwip): Add early out in NetworkUDP::parsePacket() when socket has no data.
Previously, `NetworkUDP::parsePacket()` would take the time to allocate a 1460 byte buffer to call `recvfrom()` with, immediately freeing it if there was no data read. This change has it check if there is available data via `ioctl()` with `FIONREAD` first, saving the allocation and thus significantly increasing performance in no data situations.
1 parent 0fa4aa6 commit 4871631

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

Diff for: libraries/Network/src/NetworkUdp.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,13 @@ int NetworkUDP::parsePacket() {
297297
struct sockaddr_storage si_other_storage; // enough storage for v4 and v6
298298
socklen_t slen = sizeof(sockaddr_storage);
299299
int len;
300+
if (ioctl(udp_server, FIONREAD, &len) == -1) {
301+
log_e("could not check for data in buffer length: %d", errno);
302+
return 0;
303+
}
304+
if (!len) {
305+
return 0;
306+
}
300307
char *buf = (char *)malloc(1460);
301308
if (!buf) {
302309
return 0;

0 commit comments

Comments
 (0)