From e80f6163662e31d2b2a541e1c75aa3ca6c0ce3b2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 15 Aug 2025 02:38:27 -0500 Subject: [PATCH] [esp32_ble] Optimize BLE event memory usage by eliminating std::vector overhead (#10247) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- esphome/components/esp32_ble/ble_event.h | 57 +++++++++++++++++------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/esphome/components/esp32_ble/ble_event.h b/esphome/components/esp32_ble/ble_event.h index 884fc9ba65..2c0ab1d34e 100644 --- a/esphome/components/esp32_ble/ble_event.h +++ b/esphome/components/esp32_ble/ble_event.h @@ -3,8 +3,7 @@ #ifdef USE_ESP32 #include // for offsetof -#include - +#include // for memcpy #include #include #include @@ -64,7 +63,7 @@ static_assert(offsetof(esp_ble_gap_cb_param_t, read_rssi_cmpl.remote_addr) == si // Received GAP, GATTC and GATTS events are only queued, and get processed in the main loop(). // This class stores each event with minimal memory usage. -// GAP events (99% of traffic) don't have the vector overhead. +// GAP events (99% of traffic) don't have the heap allocation overhead. // GATTC/GATTS events use heap allocation for their param and data. // // Event flow: @@ -145,16 +144,18 @@ class BLEEvent { } if (this->type_ == GATTC) { delete this->event_.gattc.gattc_param; - delete this->event_.gattc.data; + delete[] this->event_.gattc.data; this->event_.gattc.gattc_param = nullptr; this->event_.gattc.data = nullptr; + this->event_.gattc.data_len = 0; return; } if (this->type_ == GATTS) { delete this->event_.gatts.gatts_param; - delete this->event_.gatts.data; + delete[] this->event_.gatts.data; this->event_.gatts.gatts_param = nullptr; this->event_.gatts.data = nullptr; + this->event_.gatts.data_len = 0; } } @@ -209,17 +210,19 @@ class BLEEvent { esp_gattc_cb_event_t gattc_event; esp_gatt_if_t gattc_if; esp_ble_gattc_cb_param_t *gattc_param; // Heap-allocated - std::vector *data; // Heap-allocated - } gattc; // 16 bytes (pointers only) + uint8_t *data; // Heap-allocated raw buffer (manually managed) + uint16_t data_len; // Track size separately + } gattc; // NOLINTNEXTLINE(readability-identifier-naming) struct gatts_event { esp_gatts_cb_event_t gatts_event; esp_gatt_if_t gatts_if; esp_ble_gatts_cb_param_t *gatts_param; // Heap-allocated - std::vector *data; // Heap-allocated - } gatts; // 16 bytes (pointers only) - } event_; // 80 bytes + uint8_t *data; // Heap-allocated raw buffer (manually managed) + uint16_t data_len; // Track size separately + } gatts; + } event_; // 80 bytes ble_event_t type_; @@ -319,6 +322,7 @@ class BLEEvent { if (p == nullptr) { this->event_.gattc.gattc_param = nullptr; this->event_.gattc.data = nullptr; + this->event_.gattc.data_len = 0; return; // Invalid event, but we can't log in header file } @@ -336,16 +340,29 @@ class BLEEvent { // We must copy this data to ensure it remains valid when the event is processed later. switch (e) { case ESP_GATTC_NOTIFY_EVT: - this->event_.gattc.data = new std::vector(p->notify.value, p->notify.value + p->notify.value_len); - this->event_.gattc.gattc_param->notify.value = this->event_.gattc.data->data(); + this->event_.gattc.data_len = p->notify.value_len; + if (p->notify.value_len > 0) { + this->event_.gattc.data = new uint8_t[p->notify.value_len]; + memcpy(this->event_.gattc.data, p->notify.value, p->notify.value_len); + } else { + this->event_.gattc.data = nullptr; + } + this->event_.gattc.gattc_param->notify.value = this->event_.gattc.data; break; case ESP_GATTC_READ_CHAR_EVT: case ESP_GATTC_READ_DESCR_EVT: - this->event_.gattc.data = new std::vector(p->read.value, p->read.value + p->read.value_len); - this->event_.gattc.gattc_param->read.value = this->event_.gattc.data->data(); + this->event_.gattc.data_len = p->read.value_len; + if (p->read.value_len > 0) { + this->event_.gattc.data = new uint8_t[p->read.value_len]; + memcpy(this->event_.gattc.data, p->read.value, p->read.value_len); + } else { + this->event_.gattc.data = nullptr; + } + this->event_.gattc.gattc_param->read.value = this->event_.gattc.data; break; default: this->event_.gattc.data = nullptr; + this->event_.gattc.data_len = 0; break; } } @@ -358,6 +375,7 @@ class BLEEvent { if (p == nullptr) { this->event_.gatts.gatts_param = nullptr; this->event_.gatts.data = nullptr; + this->event_.gatts.data_len = 0; return; // Invalid event, but we can't log in header file } @@ -375,11 +393,18 @@ class BLEEvent { // We must copy this data to ensure it remains valid when the event is processed later. switch (e) { case ESP_GATTS_WRITE_EVT: - this->event_.gatts.data = new std::vector(p->write.value, p->write.value + p->write.len); - this->event_.gatts.gatts_param->write.value = this->event_.gatts.data->data(); + this->event_.gatts.data_len = p->write.len; + if (p->write.len > 0) { + this->event_.gatts.data = new uint8_t[p->write.len]; + memcpy(this->event_.gatts.data, p->write.value, p->write.len); + } else { + this->event_.gatts.data = nullptr; + } + this->event_.gatts.gatts_param->write.value = this->event_.gatts.data; break; default: this->event_.gatts.data = nullptr; + this->event_.gatts.data_len = 0; break; } }