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

Fix quadratic destruction complexity
This commit is contained in:
Niels Lohmann 2019-11-15 07:02:12 +01:00 committed by GitHub
commit c6e1e26018
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 12 deletions

View file

@ -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
// 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
} }
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)

View file

@ -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
// 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
} }
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)