From 57454cf97b584ba7f73ad1d67d3ab3e585fe982a Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Tue, 1 Aug 2023 18:39:35 +0200 Subject: [PATCH] Don't call `shrinkToFit()` for `deserializeXxx(JsonVariant)` --- .../JsonDeserializer/destination_types.cpp | 34 ++++++------------- .../MsgPackDeserializer/destination_types.cpp | 27 +++++---------- .../Deserialization/deserialize.hpp | 15 ++++++-- 3 files changed, 31 insertions(+), 45 deletions(-) diff --git a/extras/tests/JsonDeserializer/destination_types.cpp b/extras/tests/JsonDeserializer/destination_types.cpp index c387572d..f05173d4 100644 --- a/extras/tests/JsonDeserializer/destination_types.cpp +++ b/extras/tests/JsonDeserializer/destination_types.cpp @@ -43,11 +43,9 @@ TEST_CASE("deserializeJson(JsonVariant)") { REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.as() == "[[42]]"); - REQUIRE(spy.log() == - AllocatorLog{ - Deallocate(sizeofString("hello")), - Reallocate(sizeofPool(), sizeofArray(1) + sizeofArray(1)), - }); + REQUIRE(spy.log() == AllocatorLog{ + Deallocate(sizeofString("hello")), + }); } SECTION("variant is unbound") { @@ -70,11 +68,9 @@ TEST_CASE("deserializeJson(ElementProxy)") { REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.as() == "[[42]]"); - REQUIRE(spy.log() == - AllocatorLog{ - Deallocate(sizeofString("hello")), - Reallocate(sizeofPool(), sizeofArray(1) + sizeofArray(1)), - }); + REQUIRE(spy.log() == AllocatorLog{ + Deallocate(sizeofString("hello")), + }); } SECTION("element must be created") { @@ -82,10 +78,7 @@ TEST_CASE("deserializeJson(ElementProxy)") { REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.as() == "[\"hello\",[42]]"); - REQUIRE(spy.log() == - AllocatorLog{ - Reallocate(sizeofPool(), sizeofArray(2) + sizeofArray(1)), - }); + REQUIRE(spy.log() == AllocatorLog{}); } } @@ -100,11 +93,9 @@ TEST_CASE("deserializeJson(MemberProxy)") { REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.as() == "{\"hello\":[42]}"); - REQUIRE(spy.log() == - AllocatorLog{ - Deallocate(sizeofString("world")), - Reallocate(sizeofPool(), sizeofObject(1) + sizeofArray(1)), - }); + REQUIRE(spy.log() == AllocatorLog{ + Deallocate(sizeofString("world")), + }); } SECTION("member must be created exists") { @@ -112,9 +103,6 @@ TEST_CASE("deserializeJson(MemberProxy)") { REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.as() == "{\"hello\":\"world\",\"value\":[42]}"); - REQUIRE(spy.log() == - AllocatorLog{ - Reallocate(sizeofPool(), sizeofObject(2) + sizeofArray(1)), - }); + REQUIRE(spy.log() == AllocatorLog{}); } } diff --git a/extras/tests/MsgPackDeserializer/destination_types.cpp b/extras/tests/MsgPackDeserializer/destination_types.cpp index eea1c154..43e7e22c 100644 --- a/extras/tests/MsgPackDeserializer/destination_types.cpp +++ b/extras/tests/MsgPackDeserializer/destination_types.cpp @@ -43,11 +43,9 @@ TEST_CASE("deserializeMsgPack(JsonVariant)") { REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.as() == "[[42]]"); - REQUIRE(spy.log() == - AllocatorLog{ - Deallocate(sizeofString("hello")), - Reallocate(sizeofPool(), sizeofArray(1) + sizeofArray(1)), - }); + REQUIRE(spy.log() == AllocatorLog{ + Deallocate(sizeofString("hello")), + }); } SECTION("variant is unbound") { @@ -70,11 +68,9 @@ TEST_CASE("deserializeMsgPack(ElementProxy)") { REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.as() == "[[42]]"); - REQUIRE(spy.log() == - AllocatorLog{ - Deallocate(sizeofString("hello")), - Reallocate(sizeofPool(), sizeofArray(1) + sizeofArray(1)), - }); + REQUIRE(spy.log() == AllocatorLog{ + Deallocate(sizeofString("hello")), + }); } SECTION("element must be created exists") { @@ -82,10 +78,7 @@ TEST_CASE("deserializeMsgPack(ElementProxy)") { REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.as() == "[\"hello\",[42]]"); - REQUIRE(spy.log() == - AllocatorLog{ - Reallocate(sizeofPool(), sizeofArray(2) + sizeofArray(1)), - }); + REQUIRE(spy.log() == AllocatorLog{}); } } @@ -102,7 +95,6 @@ TEST_CASE("deserializeMsgPack(MemberProxy)") { REQUIRE(doc.as() == "{\"hello\":[42]}"); REQUIRE(spy.log() == AllocatorLog{ Deallocate(sizeofString("world")), - Reallocate(sizeofPool(), sizeofObject(2)), }); } @@ -111,9 +103,6 @@ TEST_CASE("deserializeMsgPack(MemberProxy)") { REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.as() == "{\"hello\":\"world\",\"value\":[42]}"); - REQUIRE(spy.log() == - AllocatorLog{ - Reallocate(sizeofPool(), sizeofObject(2) + sizeofArray(1)), - }); + REQUIRE(spy.log() == AllocatorLog{}); } } diff --git a/src/ArduinoJson/Deserialization/deserialize.hpp b/src/ArduinoJson/Deserialization/deserialize.hpp index bd3e9108..b8bfd51e 100644 --- a/src/ArduinoJson/Deserialization/deserialize.hpp +++ b/src/ArduinoJson/Deserialization/deserialize.hpp @@ -34,6 +34,17 @@ struct is_deserialize_destination< ResourceManager*>::value>::type> : true_type { }; +template +inline void shrinkJsonDocument(TDestination&) { + // no-op by default +} + +#if ARDUINOJSON_AUTO_SHRINK +inline void shrinkJsonDocument(JsonDocument& doc) { + doc.shrinkToFit(); +} +#endif + template