From 9ef48f815263ded88c24409594ece9bf3b2d4635 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 9 Oct 2026 18:04:16 +0200 Subject: [PATCH] Release the list of large objects once load() has built the indexes Signed-off-by: Niels Lohmann --- include/nlohmann/detail/view/image.hpp | 1 + tests/src/unit-json_view_image.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/nlohmann/detail/view/image.hpp b/include/nlohmann/detail/view/image.hpp index 49ca8dc67..d228034f7 100644 --- a/include/nlohmann/detail/view/image.hpp +++ b/include/nlohmann/detail/view/image.hpp @@ -738,6 +738,7 @@ inline void load_image(document_data& d, const std::uint8_t* image, std::size_t } } build_object_indexes(d); + std::vector().swap(d.large_objects); // (only needed while building) d.discarded = false; } diff --git a/tests/src/unit-json_view_image.cpp b/tests/src/unit-json_view_image.cpp index 8fc5a5990..d7c8f3e8b 100644 --- a/tests/src/unit-json_view_image.cpp +++ b/tests/src/unit-json_view_image.cpp @@ -373,6 +373,32 @@ TEST_CASE("json_view images: round trips") } } } + + SECTION("loading releases the list of large objects") + { + std::string text = "["; + for (int object = 0; object < 400; ++object) + { + text += object != 0 ? ",{" : "{"; + for (int i = 0; i < 128; ++i) + { + text += (i != 0 ? ",\"" : "\"") + std::to_string(i) + "\":" + std::to_string(i); + } + text += '}'; + } + text += "]"; + json_document parsed = json_document::parse(text); + const std::vector image = parsed.save(); + json_document loaded = json_document::load(image); + CHECK(loaded.root()[399]["127"] == 127); + // Parsing and loading build the same tables, and keep nothing else: + // not the positions of the objects to index (2 KiB here). The slack + // covers the nodes in the header of the document, which are sized + // differently. + parsed.shrink_to_fit(); + loaded.shrink_to_fit(); + CHECK(loaded.memory_usage() <= parsed.memory_usage() + 512); + } } TEST_CASE("json_view images: edited documents")