From 4edc715b1c86e5985a5c62882f94fbb4f621d739 Mon Sep 17 00:00:00 2001 From: Christian Stenger Date: Mon, 26 Jun 2017 13:16:21 +0200 Subject: [PATCH] AutoTest: Speed up finding parent items We normally add new results to the last added item, so processing the search for the parent from bottom to top makes more sense and avoids iterating over almost every item. Change-Id: Iede08b9c0d4c80227d2e8fea9b002354f01d5b35 Reviewed-by: hjk --- src/libs/utils/treemodel.cpp | 12 ++++++++++++ src/libs/utils/treemodel.h | 2 ++ src/plugins/autotest/testresultmodel.cpp | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/libs/utils/treemodel.cpp b/src/libs/utils/treemodel.cpp index db8923e372a..19f3fc72c48 100644 --- a/src/libs/utils/treemodel.cpp +++ b/src/libs/utils/treemodel.cpp @@ -841,6 +841,18 @@ TreeItem *TreeItem::findAnyChild(const std::function &pred) co return 0; } +TreeItem *TreeItem::reverseFindAnyChild(const std::function &pred) const +{ + auto end = m_children.rend(); + for (auto it = m_children.rbegin(); it != end; ++it) { + if (pred(*it)) + return *it; + if (TreeItem *found = (*it)->reverseFindAnyChild(pred)) + return found; + } + return nullptr; +} + void TreeItem::clear() { while (childCount() != 0) { diff --git a/src/libs/utils/treemodel.h b/src/libs/utils/treemodel.h index 60db234d3b4..d62670867be 100644 --- a/src/libs/utils/treemodel.h +++ b/src/libs/utils/treemodel.h @@ -78,6 +78,8 @@ public: void forSelectedChildren(const std::function &pred) const; void forAllChildren(const std::function &pred) const; TreeItem *findAnyChild(const std::function &pred) const; + // like findAnyChild() but processes children from bottom to top + TreeItem *reverseFindAnyChild(const std::function &pred) const; // Levels are 1-based: Child at Level 1 is an immediate child. void forChildrenAtLevel(int level, const std::function &pred) const; diff --git a/src/plugins/autotest/testresultmodel.cpp b/src/plugins/autotest/testresultmodel.cpp index c962b8a19f6..3bfdba93573 100644 --- a/src/plugins/autotest/testresultmodel.cpp +++ b/src/plugins/autotest/testresultmodel.cpp @@ -313,7 +313,7 @@ TestResultItem *TestResultModel::findParentItemFor(const TestResultItem *item, TestResultItem *currentItem = static_cast(it); return currentItem->testResult()->isDirectParentOf(result, &needsIntermediate); }; - TestResultItem *parent = static_cast(root->findAnyChild(predicate)); + TestResultItem *parent = static_cast(root->reverseFindAnyChild(predicate)); if (parent) { if (needsIntermediate) { // check if the intermediate is present already