[bluetooth_proxy] Remove unnecessary heap allocation for response object (#10104)

This commit is contained in:
J. Nick Koston 2025-08-06 15:42:04 -10:00 committed by GitHub
parent 6d66ddd68d
commit 61008bc8a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 8 deletions

View File

@ -25,12 +25,9 @@ static_assert(sizeof(((api::BluetoothLERawAdvertisement *) nullptr)->data) == 62
BluetoothProxy::BluetoothProxy() { global_bluetooth_proxy = this; } BluetoothProxy::BluetoothProxy() { global_bluetooth_proxy = this; }
void BluetoothProxy::setup() { void BluetoothProxy::setup() {
// Pre-allocate response object
this->response_ = std::make_unique<api::BluetoothLERawAdvertisementsResponse>();
// Reserve capacity but start with size 0 // Reserve capacity but start with size 0
// Reserve 50% since we'll grow naturally and flush at FLUSH_BATCH_SIZE // Reserve 50% since we'll grow naturally and flush at FLUSH_BATCH_SIZE
this->response_->advertisements.reserve(FLUSH_BATCH_SIZE / 2); this->response_.advertisements.reserve(FLUSH_BATCH_SIZE / 2);
// Don't pre-allocate pool - let it grow only if needed in busy environments // Don't pre-allocate pool - let it grow only if needed in busy environments
// Many devices in quiet areas will never need the overflow pool // Many devices in quiet areas will never need the overflow pool
@ -85,7 +82,7 @@ bool BluetoothProxy::parse_devices(const esp32_ble::BLEScanResult *scan_results,
if (!api::global_api_server->is_connected() || this->api_connection_ == nullptr) if (!api::global_api_server->is_connected() || this->api_connection_ == nullptr)
return false; return false;
auto &advertisements = this->response_->advertisements; auto &advertisements = this->response_.advertisements;
for (size_t i = 0; i < count; i++) { for (size_t i = 0; i < count; i++) {
auto &result = scan_results[i]; auto &result = scan_results[i];
@ -129,7 +126,7 @@ void BluetoothProxy::flush_pending_advertisements() {
if (this->advertisement_count_ == 0 || !api::global_api_server->is_connected() || this->api_connection_ == nullptr) if (this->advertisement_count_ == 0 || !api::global_api_server->is_connected() || this->api_connection_ == nullptr)
return; return;
auto &advertisements = this->response_->advertisements; auto &advertisements = this->response_.advertisements;
// Return any items beyond advertisement_count_ to the pool // Return any items beyond advertisement_count_ to the pool
if (advertisements.size() > this->advertisement_count_) { if (advertisements.size() > this->advertisement_count_) {
@ -143,7 +140,7 @@ void BluetoothProxy::flush_pending_advertisements() {
} }
// Send the message // Send the message
this->api_connection_->send_message(*this->response_, api::BluetoothLERawAdvertisementsResponse::MESSAGE_TYPE); this->api_connection_->send_message(this->response_, api::BluetoothLERawAdvertisementsResponse::MESSAGE_TYPE);
// Reset count - existing items will be overwritten in next batch // Reset count - existing items will be overwritten in next batch
this->advertisement_count_ = 0; this->advertisement_count_ = 0;

View File

@ -150,7 +150,7 @@ class BluetoothProxy : public esp32_ble_tracker::ESPBTDeviceListener, public Com
// BLE advertisement batching // BLE advertisement batching
std::vector<api::BluetoothLERawAdvertisement> advertisement_pool_; std::vector<api::BluetoothLERawAdvertisement> advertisement_pool_;
std::unique_ptr<api::BluetoothLERawAdvertisementsResponse> response_; api::BluetoothLERawAdvertisementsResponse response_;
// Group 3: 4-byte types // Group 3: 4-byte types
uint32_t last_advertisement_flush_time_{0}; uint32_t last_advertisement_flush_time_{0};