Unverified Commit c6e1e260 by Niels Lohmann Committed by GitHub

Merge pull request #1838 from nickaein/fix-quadratic-destruction

Fix quadratic destruction complexity
parents be61ad14 efa13c66
...@@ -1010,7 +1010,6 @@ class basic_json ...@@ -1010,7 +1010,6 @@ class basic_json
else if (t == value_t::object) else if (t == value_t::object)
{ {
stack.reserve(object->size()); stack.reserve(object->size());
for (auto&& it : *object) for (auto&& it : *object)
{ {
stack.push_back(std::move(it.second)); stack.push_back(std::move(it.second));
...@@ -1027,8 +1026,6 @@ class basic_json ...@@ -1027,8 +1026,6 @@ class basic_json
// its children to the stack to be processed later // its children to the stack to be processed later
if (current_item.is_array()) 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::move(current_item.m_value.array->begin(), current_item.m_value.array->end(),
std::back_inserter(stack)); std::back_inserter(stack));
...@@ -1036,15 +1033,16 @@ class basic_json ...@@ -1036,15 +1033,16 @@ class basic_json
} }
else if (current_item.is_object()) else if (current_item.is_object())
{ {
stack.reserve(stack.size() + current_item.m_value.object->size());
for (auto&& it : *current_item.m_value.object) for (auto&& it : *current_item.m_value.object)
{ {
stack.push_back(std::move(it.second)); 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) switch (t)
......
...@@ -15553,7 +15553,6 @@ class basic_json ...@@ -15553,7 +15553,6 @@ class basic_json
else if (t == value_t::object) else if (t == value_t::object)
{ {
stack.reserve(object->size()); stack.reserve(object->size());
for (auto&& it : *object) for (auto&& it : *object)
{ {
stack.push_back(std::move(it.second)); stack.push_back(std::move(it.second));
...@@ -15570,8 +15569,6 @@ class basic_json ...@@ -15570,8 +15569,6 @@ class basic_json
// its children to the stack to be processed later // its children to the stack to be processed later
if (current_item.is_array()) 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::move(current_item.m_value.array->begin(), current_item.m_value.array->end(),
std::back_inserter(stack)); std::back_inserter(stack));
...@@ -15579,15 +15576,16 @@ class basic_json ...@@ -15579,15 +15576,16 @@ class basic_json
} }
else if (current_item.is_object()) else if (current_item.is_object())
{ {
stack.reserve(stack.size() + current_item.m_value.object->size());
for (auto&& it : *current_item.m_value.object) for (auto&& it : *current_item.m_value.object)
{ {
stack.push_back(std::move(it.second)); 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) switch (t)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment