Merge pull request #1838 from nickaein/fix-quadratic-destruction
Fix quadratic destruction complexity
diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp
index 5d07b8f..eecff7f 100644
--- a/include/nlohmann/json.hpp
+++ b/include/nlohmann/json.hpp
@@ -1010,7 +1010,6 @@
else if (t == value_t::object)
{
stack.reserve(object->size());
-
for (auto&& it : *object)
{
stack.push_back(std::move(it.second));
@@ -1027,8 +1026,6 @@
// 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,15 +1033,16 @@
}
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));
}
+
+ 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 726a963..f9be0b4 100644
--- a/single_include/nlohmann/json.hpp
+++ b/single_include/nlohmann/json.hpp
@@ -15553,7 +15553,6 @@
else if (t == value_t::object)
{
stack.reserve(object->size());
-
for (auto&& it : *object)
{
stack.push_back(std::move(it.second));
@@ -15570,8 +15569,6 @@
// 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,15 +15576,16 @@
}
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));
}
+
+ 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)