From 0f3ec003bb141f4da0bedf4eaaed9feaf7ccc2ab Mon Sep 17 00:00:00 2001 From: Isaac Nickaein Date: Tue, 12 Nov 2019 22:19:55 +0330 Subject: [PATCH 1/3] Remove harmful vector::reserve during destruction (#1837) --- include/nlohmann/json.hpp | 7 ------- single_include/nlohmann/json.hpp | 7 ------- 2 files changed, 14 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 5d07b8f9..b60122cc 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1004,13 +1004,10 @@ class basic_json // move the top-level items to stack if (t == value_t::array) { - stack.reserve(array->size()); std::move(array->begin(), array->end(), std::back_inserter(stack)); } else if (t == value_t::object) { - stack.reserve(object->size()); - for (auto&& it : *object) { stack.push_back(std::move(it.second)); @@ -1027,8 +1024,6 @@ class basic_json // its children to the stack to be processed later if (current_item.is_array()) { - stack.reserve(stack.size() + current_item.m_value.array->size()); - std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(), std::back_inserter(stack)); @@ -1036,8 +1031,6 @@ class basic_json } else if (current_item.is_object()) { - stack.reserve(stack.size() + current_item.m_value.object->size()); - for (auto&& it : *current_item.m_value.object) { stack.push_back(std::move(it.second)); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 726a9632..d2d85b3d 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -15547,13 +15547,10 @@ class basic_json // move the top-level items to stack if (t == value_t::array) { - stack.reserve(array->size()); std::move(array->begin(), array->end(), std::back_inserter(stack)); } else if (t == value_t::object) { - stack.reserve(object->size()); - for (auto&& it : *object) { stack.push_back(std::move(it.second)); @@ -15570,8 +15567,6 @@ class basic_json // its children to the stack to be processed later if (current_item.is_array()) { - stack.reserve(stack.size() + current_item.m_value.array->size()); - std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(), std::back_inserter(stack)); @@ -15579,8 +15574,6 @@ class basic_json } else if (current_item.is_object()) { - stack.reserve(stack.size() + current_item.m_value.object->size()); - for (auto&& it : *current_item.m_value.object) { stack.push_back(std::move(it.second)); From 948f98cf4a083e9c9c034fb3cbbb532debe5ceb8 Mon Sep 17 00:00:00 2001 From: Isaac Nickaein Date: Tue, 12 Nov 2019 22:24:11 +0330 Subject: [PATCH 2/3] Cleanups Make our intent more clear in destruction --- include/nlohmann/json.hpp | 5 ++++- single_include/nlohmann/json.hpp | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index b60122cc..5a66a860 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1035,9 +1035,12 @@ class basic_json { stack.push_back(std::move(it.second)); } + + current_item.m_value.object->clear(); } - // current_item is destroyed here + // it's now safe that current_item get destructed + // since it doesn't have any children } switch (t) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index d2d85b3d..8acb1cc0 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -15578,9 +15578,12 @@ class basic_json { stack.push_back(std::move(it.second)); } + + current_item.m_value.object->clear(); } - // current_item is destroyed here + // it's now safe that current_item get destructed + // since it doesn't have any children } switch (t) From efa13c663d8098a453c6e46769ee05165ecf47f9 Mon Sep 17 00:00:00 2001 From: Isaac Nickaein Date: Wed, 13 Nov 2019 15:23:08 +0330 Subject: [PATCH 3/3] Reserve stack only for top-level items --- include/nlohmann/json.hpp | 2 ++ single_include/nlohmann/json.hpp | 2 ++ 2 files changed, 4 insertions(+) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 5a66a860..eecff7fc 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1004,10 +1004,12 @@ class basic_json // move the top-level items to stack if (t == value_t::array) { + stack.reserve(array->size()); std::move(array->begin(), array->end(), std::back_inserter(stack)); } else if (t == value_t::object) { + stack.reserve(object->size()); for (auto&& it : *object) { stack.push_back(std::move(it.second)); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 8acb1cc0..f9be0b4e 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -15547,10 +15547,12 @@ class basic_json // move the top-level items to stack if (t == value_t::array) { + stack.reserve(array->size()); std::move(array->begin(), array->end(), std::back_inserter(stack)); } else if (t == value_t::object) { + stack.reserve(object->size()); for (auto&& it : *object) { stack.push_back(std::move(it.second));