From 42a1f6922fe78b049c00b9494542afe1bf1cb0c5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 7 Jul 2025 16:16:48 -0500 Subject: [PATCH] Eliminate bluetooth_proxy guard variable to save 8 bytes RAM (#9343) --- .../components/bluetooth_proxy/bluetooth_proxy.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp index 98f11fac7a..a5e8ec0860 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp @@ -58,10 +58,15 @@ bool BluetoothProxy::parse_device(const esp32_ble_tracker::ESPBTDevice &device) // 16 advertisements × 80 bytes (worst case) = 1280 bytes out of ~1320 bytes usable payload // This achieves ~97% WiFi MTU utilization while staying under the limit static constexpr size_t FLUSH_BATCH_SIZE = 16; -static std::vector &get_batch_buffer() { - static std::vector batch_buffer; - return batch_buffer; -} + +namespace { +// Batch buffer in anonymous namespace to avoid guard variable (saves 8 bytes) +// This is initialized at program startup before any threads +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) +std::vector batch_buffer; +} // namespace + +static std::vector &get_batch_buffer() { return batch_buffer; } bool BluetoothProxy::parse_devices(const esp32_ble::BLEScanResult *scan_results, size_t count) { if (!api::global_api_server->is_connected() || this->api_connection_ == nullptr || !this->raw_advertisements_)