diff --git a/esphome/components/esp32/preferences.cpp b/esphome/components/esp32/preferences.cpp index 08439746b6..4e0bb68133 100644 --- a/esphome/components/esp32/preferences.cpp +++ b/esphome/components/esp32/preferences.cpp @@ -181,7 +181,8 @@ class ESP32Preferences : public ESPPreferences { if (actual_len != to_save.len) { return true; } - auto stored_data = std::make_unique(actual_len); + // Most preferences are small, use stack buffer with heap fallback for large ones + SmallBufferWithHeapFallback<256> stored_data(actual_len); err = nvs_get_blob(nvs_handle, key_str, stored_data.get(), &actual_len); if (err != 0) { ESP_LOGV(TAG, "nvs_get_blob('%s') failed: %s", key_str, esp_err_to_name(err)); diff --git a/esphome/components/libretiny/preferences.cpp b/esphome/components/libretiny/preferences.cpp index 68bc279767..978dcce3fa 100644 --- a/esphome/components/libretiny/preferences.cpp +++ b/esphome/components/libretiny/preferences.cpp @@ -166,8 +166,8 @@ class LibreTinyPreferences : public ESPPreferences { return true; } - // Allocate buffer on heap to avoid stack allocation for large data - auto stored_data = std::make_unique(kv.value_len); + // Most preferences are small, use stack buffer with heap fallback for large ones + SmallBufferWithHeapFallback<256> stored_data(kv.value_len); fdb_blob_make(&this->blob, stored_data.get(), kv.value_len); size_t actual_len = fdb_kv_get_blob(db, key_str, &this->blob); if (actual_len != kv.value_len) {