[bme68x_bsec2] Store trigger time as member to avoid SBO overflow

The set_timeout lambda captured [this, curr_time_ns] (4 + 8 = 12
bytes), exceeding the std::function small buffer optimization
threshold (8 bytes on 32-bit) and forcing a heap allocation every
measurement cycle (every 3s in LP mode).

Store curr_time_ns as a class member and capture only [this] (4
bytes), which fits inline in the SBO. Trades 8 bytes of permanent
member storage to eliminate a heap alloc+free cycle per measurement.
This commit is contained in:
J. Nick Koston
2026-03-19 00:22:31 -10:00
parent 0a3393bed3
commit d4d8e92243
2 changed files with 3 additions and 1 deletions
@@ -279,7 +279,8 @@ void BME68xBSEC2Component::run_() {
uint32_t meas_dur = 0;
meas_dur = bme68x_get_meas_dur(this->op_mode_, &bme68x_conf, &this->bme68x_);
ESP_LOGV(TAG, "Queueing read in %uus", meas_dur);
this->set_timeout("read", meas_dur / 1000, [this, curr_time_ns]() { this->read_(curr_time_ns); });
this->trigger_time_ns_ = curr_time_ns;
this->set_timeout("read", meas_dur / 1000, [this]() { this->read_(this->trigger_time_ns_); });
} else {
ESP_LOGV(TAG, "Measurement not required");
this->read_(curr_time_ns);
@@ -116,6 +116,7 @@ class BME68xBSEC2Component : public Component {
int8_t bme68x_status_{BME68X_OK};
int64_t last_time_ms_{0};
int64_t trigger_time_ns_{0}; // Stored for set_timeout lambda to avoid exceeding std::function SBO
uint32_t millis_overflow_counter_{0};
std::queue<std::function<void()>> queue_;