mirror of
https://github.com/esphome/esphome.git
synced 2024-11-30 02:34:12 +01:00
Allocate smaller amount of buffer for JSON (#3384)
This commit is contained in:
parent
6bac551d9f
commit
93b628d9a8
1 changed files with 7 additions and 7 deletions
|
@ -23,13 +23,13 @@ std::string build_json(const json_build_t &f) {
|
|||
#ifdef USE_ESP8266
|
||||
const size_t free_heap = ESP.getMaxFreeBlockSize(); // NOLINT(readability-static-accessed-through-instance)
|
||||
#elif defined(USE_ESP32)
|
||||
const size_t free_heap = heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL);
|
||||
const size_t free_heap = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
|
||||
#endif
|
||||
|
||||
const size_t request_size = std::min(free_heap - 2048, (size_t) 5120);
|
||||
const size_t request_size = std::min(free_heap, (size_t) 512);
|
||||
|
||||
DynamicJsonDocument json_document(request_size);
|
||||
if (json_document.memoryPool().buffer() == nullptr) {
|
||||
if (json_document.capacity() == 0) {
|
||||
ESP_LOGE(TAG, "Could not allocate memory for JSON document! Requested %u bytes, largest free heap block: %u bytes",
|
||||
request_size, free_heap);
|
||||
return "{}";
|
||||
|
@ -37,7 +37,7 @@ std::string build_json(const json_build_t &f) {
|
|||
JsonObject root = json_document.to<JsonObject>();
|
||||
f(root);
|
||||
json_document.shrinkToFit();
|
||||
|
||||
ESP_LOGV(TAG, "Size after shrink %u bytes", json_document.capacity());
|
||||
std::string output;
|
||||
serializeJson(json_document, output);
|
||||
return output;
|
||||
|
@ -51,13 +51,13 @@ void parse_json(const std::string &data, const json_parse_t &f) {
|
|||
#ifdef USE_ESP8266
|
||||
const size_t free_heap = ESP.getMaxFreeBlockSize(); // NOLINT(readability-static-accessed-through-instance)
|
||||
#elif defined(USE_ESP32)
|
||||
const size_t free_heap = heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL);
|
||||
const size_t free_heap = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
|
||||
#endif
|
||||
bool pass = false;
|
||||
size_t request_size = std::min(free_heap - 2048, (size_t)(data.size() * 1.5));
|
||||
size_t request_size = std::min(free_heap, (size_t)(data.size() * 1.5));
|
||||
do {
|
||||
DynamicJsonDocument json_document(request_size);
|
||||
if (json_document.memoryPool().buffer() == nullptr) {
|
||||
if (json_document.capacity() == 0) {
|
||||
ESP_LOGE(TAG, "Could not allocate memory for JSON document! Requested %u bytes, free heap: %u", request_size,
|
||||
free_heap);
|
||||
return;
|
||||
|
|
Loading…
Reference in a new issue