[socket] Fast path for TCP_NODELAY bypass lwip_setsockopt overhead

On ESP32 with CONFIG_LWIP_TCPIP_CORE_LOCKING, bypass lwip_setsockopt()
for TCP_NODELAY by directly modifying tcp_pcb->flags under the TCPIP
core lock. This eliminates ~1091 bytes of overhead per call (socket
lookups, hook, switch cascade, refcounting) for what is just a single
bit flip.

The API frame helper toggles Nagle's algorithm on every message send
via set_nodelay_for_message(), making this a hot path. The fast path
reduces set_nodelay_raw_ from calling the full lwip_setsockopt to just
acquiring the mutex, loading 3 pointers, and flipping the TF_NODELAY
bit.

Only enabled when both USE_LWIP_FAST_SELECT (cached lwip_sock pointer)
and CONFIG_LWIP_TCPIP_CORE_LOCKING (real mutex protection) are
available. Falls back to the standard setsockopt call otherwise.
This commit is contained in:
J. Nick Koston
2026-03-10 22:08:22 -10:00
parent d0f37ae694
commit c530e52669
4 changed files with 37 additions and 0 deletions
@@ -56,6 +56,15 @@ class BSDSocketImpl {
return ::getsockopt(this->fd_, level, optname, optval, optlen);
}
int setsockopt(int level, int optname, const void *optval, socklen_t optlen) {
#if defined(USE_LWIP_FAST_SELECT) && defined(CONFIG_LWIP_TCPIP_CORE_LOCKING)
// Fast path for TCP_NODELAY: directly set the pcb flag under the TCPIP core lock,
// bypassing lwip_setsockopt overhead (socket lookups, hook, switch cascade, refcounting).
if (level == IPPROTO_TCP && optname == TCP_NODELAY && optlen == sizeof(int)) {
LwIPLock lock;
if (esphome_lwip_set_nodelay(this->cached_sock_, *reinterpret_cast<const int *>(optval) != 0))
return 0;
}
#endif
return ::setsockopt(this->fd_, level, optname, optval, optlen);
}
int listen(int backlog) { return ::listen(this->fd_, backlog); }
@@ -52,6 +52,15 @@ class LwIPSocketImpl {
return lwip_getsockopt(this->fd_, level, optname, optval, optlen);
}
int setsockopt(int level, int optname, const void *optval, socklen_t optlen) {
#if defined(USE_LWIP_FAST_SELECT) && defined(CONFIG_LWIP_TCPIP_CORE_LOCKING)
// Fast path for TCP_NODELAY: directly set the pcb flag under the TCPIP core lock,
// bypassing lwip_setsockopt overhead (socket lookups, hook, switch cascade, refcounting).
if (level == IPPROTO_TCP && optname == TCP_NODELAY && optlen == sizeof(int)) {
LwIPLock lock;
if (esphome_lwip_set_nodelay(this->cached_sock_, *reinterpret_cast<const int *>(optval) != 0))
return 0;
}
#endif
return lwip_setsockopt(this->fd_, level, optname, optval, optlen);
}
int listen(int backlog) { return lwip_listen(this->fd_, backlog); }
+12
View File
@@ -112,6 +112,7 @@
// LwIP headers must come first — they define netconn_callback, struct lwip_sock, etc.
#include <lwip/api.h>
#include <lwip/priv/sockets_priv.h>
#include <lwip/tcp.h>
// FreeRTOS include paths differ: ESP-IDF uses freertos/ prefix, LibreTiny does not
#ifdef USE_ESP32
#include <freertos/FreeRTOS.h>
@@ -216,6 +217,17 @@ void esphome_lwip_hook_socket(struct lwip_sock *sock) {
sock->conn->callback = esphome_socket_event_callback;
}
bool esphome_lwip_set_nodelay(struct lwip_sock *sock, bool enable) {
if (sock == NULL || sock->conn == NULL || sock->conn->pcb.tcp == NULL)
return false;
if (enable) {
tcp_nagle_disable(sock->conn->pcb.tcp);
} else {
tcp_nagle_enable(sock->conn->pcb.tcp);
}
return true;
}
// Wake the main loop from another FreeRTOS task. NOT ISR-safe.
void esphome_lwip_wake_main_loop(void) {
TaskHandle_t task = s_main_loop_task;
+7
View File
@@ -66,6 +66,13 @@ void esphome_lwip_wake_main_loop(void);
/// @param px_higher_priority_task_woken Set to pdTRUE if a context switch is needed.
void esphome_lwip_wake_main_loop_from_isr(int *px_higher_priority_task_woken);
/// Set or clear TCP_NODELAY on a socket's tcp_pcb directly.
/// Must be called with the TCPIP core lock held (LwIPLock in C++).
/// This bypasses lwip_setsockopt() overhead (socket lookups, switch cascade,
/// hooks, refcounting) — just a direct pcb->flags bit set/clear.
/// Returns true if successful, false if sock/conn/pcb is NULL.
bool esphome_lwip_set_nodelay(struct lwip_sock *sock, bool enable);
/// Wake the main loop task from any context (ISR, thread, or main loop).
/// ESP32-only: uses xPortInIsrContext() to detect ISR context.
/// LibreTiny lacks IRAM_ATTR support needed for ISR-safe paths.