jbeder / yaml-cpp

A YAML parser and emitter in C++
MIT License
5.09k stars 1.83k forks source link

Excessive memory use when loading into nodes #1179

Closed Bascy closed 11 months ago

Bascy commented 1 year ago

I'm testing this library to see if I can use it on a ESP32 to parse my yaml config files ...

It is all working, but what really surprises me is the fact that loading a yaml file of 5800 bytes takes a massive amount of heap memory: 163Kb, that is an amount of memory that I cannot afford to use on my ESP32.

In comparison, if I convert the yaml file online to json (8000 bytes) and read this with ArduinoJson library, it uses less than 6000 bytes of heap

Is this normal behaviour of this library or am I missing something?

Logging:

[    65][I][main.cpp:103] setup(): Testing YAML parser
[    67][I][main.cpp:72] testYaml(): Heap before Load 311852
[    74][I][nodebuilder.cpp:34] OnDocumentStart(): Heap before 299592
[   438][I][nodebuilder.cpp:38] OnDocumentEnd(): Heap afters 116068
[   439][I][main.cpp:74] testYaml(): Heap2 after Load 126144
[  1458][I][main.cpp:83] testJson(): Json Heap before 310996
[  1469][I][main.cpp:92] testJson(): Json Heap after parsing 300980
[  1469][I][main.cpp:94] testJson(): Json Heap after shrink 305320

Code:

void testYaml() {
  std::stringstream stream(YAML_TEST);
  log_i("Heap before Load %u", ESP.getFreeHeap());
  YAML::Node config = YAML::Load(stream);
  log_i("Heap2 after Load %u", ESP.getFreeHeap());
}

void testJson() {
  log_i("Json Heap before %u", ESP.getFreeHeap());
  DynamicJsonDocument doc{10000};

  DeserializationError error = deserializeJson(doc, JSON_TEST);
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    return;
  }
  log_i("Json Heap after parsing %u", ESP.getFreeHeap());
  doc.shrinkToFit();
  log_i("Json Heap after shrink %u", ESP.getFreeHeap());
}

void setup() {
  testYaml();
  testJson();
}