Commit 32eeca8d authored by Daniel McArdle's avatar Daniel McArdle Committed by Commit Bot

Improve speed of QueryNodeList::RemoveEmptySubnodes

Reduces complexity from O(n^2) to O(n).  Prior to this commit, we were
performing an O(n) vector::erase for each empty child, say O(n) of
them. Now, we do an amortized constant vector::push_back for each
child to keep, along with O(n) constant-time vector::swap operations.

Bug: 1014388
Change-Id: I4e614463c5ac207dd036876a54e65cca8ce0ed25
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1863154
Commit-Queue: Dan McArdle <dmcardle@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#706175}
parent df40ea02
...@@ -190,17 +190,19 @@ void QueryNodeList::AddChild(std::unique_ptr<QueryNode> node) { ...@@ -190,17 +190,19 @@ void QueryNodeList::AddChild(std::unique_ptr<QueryNode> node) {
} }
void QueryNodeList::RemoveEmptySubnodes() { void QueryNodeList::RemoveEmptySubnodes() {
QueryNodeVector kept_children;
for (size_t i = 0; i < children_.size(); ++i) { for (size_t i = 0; i < children_.size(); ++i) {
if (children_[i]->IsWord()) if (children_[i]->IsWord()) {
kept_children.push_back(std::move(children_[i]));
continue; continue;
}
QueryNodeList* list_node = static_cast<QueryNodeList*>(children_[i].get()); QueryNodeList* list_node = static_cast<QueryNodeList*>(children_[i].get());
list_node->RemoveEmptySubnodes(); list_node->RemoveEmptySubnodes();
if (list_node->children()->empty()) { if (!list_node->children()->empty())
children_.erase(children_.begin() + i); kept_children.push_back(std::move(children_[i]));
--i;
}
} }
children_.swap(kept_children);
} }
int QueryNodeList::AppendToSQLiteQuery(base::string16* query) const { int QueryNodeList::AppendToSQLiteQuery(base::string16* query) const {
......
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