Last active
October 24, 2019 12:53
-
-
Save mcspr/901a431696b5e207bf33a0bd1246a91f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ArduinoJson.h> | |
#include <iostream> | |
int main(int argc, char **argv) { | |
DynamicJsonBuffer buffer(1024); | |
JsonObject& shared = buffer.createObject(); | |
JsonObject& a = buffer.createObject(); | |
JsonObject& b = buffer.createObject(); | |
shared["these"] = "strings"; | |
shared["are"] = "shared"; | |
a["shared"] = shared; | |
b["shared"] = shared; | |
shared["and"] = "this"; | |
std::string out_a; | |
a.printTo(out_a); | |
std::cout << out_a << std::endl; | |
std::string out_b; | |
a.printTo(out_b); | |
std::cout << out_b << std::endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#include <chrono> | |
#include <thread> | |
#include "ArduinoJson.h" | |
int main(int argc, char **argv) { | |
DynamicJsonBuffer buffer(1024); | |
JsonObject& root = buffer.createObject(); | |
JsonObject& shared = buffer.createObject(); | |
JsonObject& new_root = root.createNestedObject("new_root"); | |
new_root["shared"] = shared; | |
shared["these"] = "strings"; | |
shared["are"] = "shared"; | |
std::string out; | |
std::string buf; | |
for (int count=0; count < 1000; ++count) { | |
buf = std::to_string(count); | |
new_root["count"] = buf.c_str(); | |
new_root["const"] = "const string"; | |
new_root["count_int"] = count; | |
new_root.printTo(out); | |
std::cout << "size:" << buffer.size() << " | data:" << out << std::endl; | |
out = ""; | |
std::this_thread::sleep_for(std::chrono::milliseconds(200)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ git diff | |
diff --git a/src/ArduinoJson/DynamicJsonBuffer.hpp b/src/ArduinoJson/DynamicJsonBuffer.hpp | |
index 7fe2b68..4a37aa2 100644 | |
--- a/src/ArduinoJson/DynamicJsonBuffer.hpp | |
+++ b/src/ArduinoJson/DynamicJsonBuffer.hpp | |
@@ -7,6 +7,7 @@ | |
#include "JsonBufferBase.hpp" | |
#include <stdlib.h> | |
+#include <stdio.h> | |
#if defined(__clang__) | |
#pragma clang diagnostic push | |
@@ -23,9 +24,12 @@ namespace Internals { | |
class DefaultAllocator { | |
public: | |
void* allocate(size_t size) { | |
- return malloc(size); | |
+ void *ptr = malloc(size); | |
+ printf("+ %p malloc(%zu)\n", ptr, size); | |
+ return ptr; | |
} | |
void deallocate(void* pointer) { | |
+ printf("- %p free()\n", pointer); | |
free(pointer); | |
} | |
}; | |
$ ./runme2 | |
+ 0x7fffd3f00e70 malloc(1048) | |
size:272 | data:{"shared":{"these":"strings","are":"shared"},"count":"0","const":"const string","count_int":0} | |
size:272 | data:{"shared":{"these":"strings","are":"shared"},"count":"1","const":"const string","count_int":1} | |
size:272 | data:{"shared":{"these":"strings","are":"shared"},"count":"2","const":"const string","count_int":2} | |
size:272 | data:{"shared":{"these":"strings","are":"shared"},"count":"3","const":"const string","count_int":3} | |
size:272 | data:{"shared":{"these":"strings","are":"shared"},"count":"4","const":"const string","count_int":4} | |
size:272 | data:{"shared":{"these":"strings","are":"shared"},"count":"5","const":"const string","count_int":5} | |
size:272 | data:{"shared":{"these":"strings","are":"shared"},"count":"6","const":"const string","count_int":6} | |
size:272 | data:{"shared":{"these":"strings","are":"shared"},"count":"7","const":"const string","count_int":7} | |
size:272 | data:{"shared":{"these":"strings","are":"shared"},"count":"8","const":"const string","count_int":8} | |
size:272 | data:{"shared":{"these":"strings","are":"shared"},"count":"9","const":"const string","count_int":9} | |
size:272 | data:{"shared":{"these":"strings","are":"shared"},"count":"10","const":"const string","count_int":10} | |
size:272 | data:{"shared":{"these":"strings","are":"shared"},"count":"11","const":"const string","count_int":11} | |
size:272 | data:{"shared":{"these":"strings","are":"shared"},"count":"12","const":"const string","count_int":12} | |
size:272 | data:{"shared":{"these":"strings","are":"shared"},"count":"13","const":"const string","count_int":13} | |
size:272 | data:{"shared":{"these":"strings","are":"shared"},"count":"14","const":"const string","count_int":14} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- run.cpp 2019-10-24 15:51:21.364712300 +0300 | |
+++ run2.cpp 2019-10-24 15:52:38.721239600 +0300 | |
@@ -10,9 +10,6 @@ | |
JsonObject& root = buffer.createObject(); | |
JsonObject& shared = buffer.createObject(); | |
- JsonObject& new_root = root.createNestedObject("new_root"); | |
- new_root["shared"] = shared; | |
- | |
shared["these"] = "strings"; | |
shared["are"] = "shared"; | |
@@ -20,6 +17,9 @@ | |
std::string out; | |
std::string buf; | |
for (int count=0; count < 1000; ++count) { | |
+ JsonObject& new_root = root.createNestedObject("new_root"); | |
+ new_root["shared"] = shared; | |
+ | |
buf = std::to_string(count); | |
new_root["count"] = buf.c_str(); | |
new_root["const"] = "const string"; | |
@@ -27,6 +27,7 @@ | |
new_root.printTo(out); | |
std::cout << "size:" << buffer.size() << " | data:" << out << std::endl; | |
+ root.remove("new_root"); | |
out = ""; | |
std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./runme3 | |
+ 0x7fffe7e76e70 malloc(1048) | |
size:272 | data:{"shared":{"these":"strings","are":"shared"},"count":"0","const":"const string","count_int":0} | |
size:416 | data:{"shared":{"these":"strings","are":"shared"},"count":"1","const":"const string","count_int":1} | |
size:560 | data:{"shared":{"these":"strings","are":"shared"},"count":"2","const":"const string","count_int":2} | |
size:704 | data:{"shared":{"these":"strings","are":"shared"},"count":"3","const":"const string","count_int":3} | |
size:848 | data:{"shared":{"these":"strings","are":"shared"},"count":"4","const":"const string","count_int":4} | |
size:992 | data:{"shared":{"these":"strings","are":"shared"},"count":"5","const":"const string","count_int":5} | |
+ 0x7fffe7e777b0 malloc(2072) | |
size:1136 | data:{"shared":{"these":"strings","are":"shared"},"count":"6","const":"const string","count_int":6} | |
size:1280 | data:{"shared":{"these":"strings","are":"shared"},"count":"7","const":"const string","count_int":7} | |
size:1424 | data:{"shared":{"these":"strings","are":"shared"},"count":"8","const":"const string","count_int":8} | |
size:1568 | data:{"shared":{"these":"strings","are":"shared"},"count":"9","const":"const string","count_int":9} | |
size:1712 | data:{"shared":{"these":"strings","are":"shared"},"count":"10","const":"const string","count_int":10} | |
size:1856 | data:{"shared":{"these":"strings","are":"shared"},"count":"11","const":"const string","count_int":11} | |
size:2000 | data:{"shared":{"these":"strings","are":"shared"},"count":"12","const":"const string","count_int":12} | |
size:2144 | data:{"shared":{"these":"strings","are":"shared"},"count":"13","const":"const string","count_int":13} | |
size:2288 | data:{"shared":{"these":"strings","are":"shared"},"count":"14","const":"const string","count_int":14} | |
size:2432 | data:{"shared":{"these":"strings","are":"shared"},"count":"15","const":"const string","count_int":15} | |
size:2576 | data:{"shared":{"these":"strings","are":"shared"},"count":"16","const":"const string","count_int":16} | |
size:2720 | data:{"shared":{"these":"strings","are":"shared"},"count":"17","const":"const string","count_int":17} | |
size:2864 | data:{"shared":{"these":"strings","are":"shared"},"count":"18","const":"const string","count_int":18} | |
size:3008 | data:{"shared":{"these":"strings","are":"shared"},"count":"19","const":"const string","count_int":19} | |
+ 0x7fffe7e77fd0 malloc(4120) | |
size:3152 | data:{"shared":{"these":"strings","are":"shared"},"count":"20","const":"const string","count_int":20} | |
size:3296 | data:{"shared":{"these":"strings","are":"shared"},"count":"21","const":"const string","count_int":21} | |
size:3440 | data:{"shared":{"these":"strings","are":"shared"},"count":"22","const":"const string","count_int":22} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ git clone -b v5.13.5 https://github.com/bblanchon/ArduinoJson | |
$ cd ArduinoJson | |
...get runme.cpp here... | |
$ g++ -I./ -o runme runme.cpp | |
$ ./runme | |
{"shared":{"these":"strings","are":"shared","and":"this"}} | |
{"shared":{"these":"strings","are":"shared","and":"this"}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment