From 7a9fba1026d725818a487abc6854dccff5979cd2 Mon Sep 17 00:00:00 2001 From: Chihun Jeong Date: Fri, 10 Mar 2023 17:28:44 +0900 Subject: [PATCH] Aurum: Improve performance of findElements command Change-Id: Iecd9e98bab5e48dccb9a2475f745406fad6c1f46 --- libaurum/inc/AurumXML.h | 4 +- libaurum/inc/Comparer.h | 12 ++++-- libaurum/src/AurumXML.cc | 6 +-- libaurum/src/Comparer.cc | 48 ++++++++++------------ libaurum/src/UiDevice.cc | 5 ++- libaurum/src/UiObject.cc | 4 +- .../src/Commands/FindElementsCommand.cc | 1 - 7 files changed, 39 insertions(+), 41 deletions(-) diff --git a/libaurum/inc/AurumXML.h b/libaurum/inc/AurumXML.h index 025508e..9bc84a4 100644 --- a/libaurum/inc/AurumXML.h +++ b/libaurum/inc/AurumXML.h @@ -81,11 +81,11 @@ public: * * @param xpath * - * @return the list of found AccessibleNode vector + * @param ret vector contains objects * * @since_tizen 7.0 */ - std::vector> findObjects(std::string xpath, bool earlyReturn = false); + void findObjects(std::vector> &ret, std::string xpath, bool earlyReturn = false); private: /** diff --git a/libaurum/inc/Comparer.h b/libaurum/inc/Comparer.h index eb1f23b..66cde43 100644 --- a/libaurum/inc/Comparer.h +++ b/libaurum/inc/Comparer.h @@ -82,16 +82,16 @@ public: * it finds focused window then start to find object as it root. * Finds all objects to the end of tree. * + * @param[in] ret vector where found objects stored * @param[in] device @UiDevice * @param[in] selector @UiSelector * @param[in] root @AccessibleNode root object(focused window on current state) * @param[in] earlyReturn find all object or not (default = false) * - * @return AccessibleNode if found, else nullptr * * @since_tizen 6.5 */ - static std::vector> findObjects( + static void findObjects(std::vector> &ret, const std::shared_ptr& device, const std::shared_ptr& selector, const std::shared_ptr& root, bool earlyReturn = false); @@ -101,17 +101,20 @@ private: * * @brief Starts find object from root. * + * @param[in] ret vector where found objects stored * @param[in] root @AccessibleNode * * @since_tizen 6.5 */ - std::vector> findObjects(const std::shared_ptr& root); + void findObjects(std::vector> &ret, + const std::shared_ptr& root); /** * @internal * * @brief It updates all partialMatches and traverse tree till given depth to find objects * + * @param[in] ret vector where found objects stored * @param[in] root @AccessibleNode * @param[in] index node index * @param[in] depth tree depth @@ -119,7 +122,8 @@ private: * * @since_tizen 6.5 */ - std::vector> findObjects( + void findObjects( + std::vector> &ret, const std::shared_ptr& root, const int &index, const int &depth, std::list> &partialMatches); diff --git a/libaurum/src/AurumXML.cc b/libaurum/src/AurumXML.cc index e6ef9f6..7da5c37 100644 --- a/libaurum/src/AurumXML.cc +++ b/libaurum/src/AurumXML.cc @@ -213,11 +213,9 @@ std::string AurumXML::getXPath(const std::shared_ptr& node) return "NotSupported"; } -std::vector> AurumXML::findObjects( +void AurumXML::findObjects(std::vector> &ret, std::string xpath, bool earlyReturn) { - std::vector> ret; - createXMLtree(); LOGI("xpath %s earlyReturn %d", xpath.c_str(), earlyReturn); @@ -239,6 +237,4 @@ std::vector> AurumXML::findObjects( } catch (const xpath_exception &e) { LOGI("findObjects Error: %s", e.what()); } - - return ret; } diff --git a/libaurum/src/Comparer.cc b/libaurum/src/Comparer.cc index ec1e402..0262213 100644 --- a/libaurum/src/Comparer.cc +++ b/libaurum/src/Comparer.cc @@ -31,14 +31,16 @@ std::shared_ptr Comparer::findObject(const std::shared_ptr& selector, const std::shared_ptr& root) { - std::vector> ret = findObjects(device, selector, root, true); + std::vector> ret; + findObjects(ret, device, selector, root, true); if (ret.size() > 0) return std::move(ret[0]); else return nullptr; } -std::vector> Comparer::findObjects(const std::shared_ptr& device, +void Comparer::findObjects(std::vector> &ret, + const std::shared_ptr& device, const std::shared_ptr& selector, const std::shared_ptr& root, bool earlyReturn) { @@ -46,48 +48,46 @@ std::vector> Comparer::findObjects(const std::sh LOGI("findObjects selector(%s) from (type:%s style:%s, role:%s, text:%s) earlyReturn:%d", selector->description().c_str(), root->getType().c_str(), root->getStyle().c_str(), root->getRole().c_str(), root->getText().c_str(), earlyReturn); if (selector->mParent) { - auto ret = Comparer::findObjects(device, selector->mParent, root); - std::vector> merged{}; + + // TODO: Optimize findObjects() when selector has a parent + std::vector> ret; + Comparer::findObjects(ret, device, selector->mParent, root); for (const auto &node : ret) { - auto tmp = comparer.findObjects(node); - std::move(std::begin(tmp), std::end(tmp), std::back_inserter(merged)); + comparer.findObjects(ret, node); } - return merged; + + return; } if (selector->mMatchXPath) { - std::vector> merged{}; - std::string pkg = root->getPkg(); auto XMLDoc = AccessibleWatcher::getInstance()->getXMLDoc(pkg); - if (XMLDoc.get() == nullptr) return merged; + if (XMLDoc.get() == nullptr) return; - auto tmp = XMLDoc->findObjects(selector->mXPath, earlyReturn); - std::move(std::begin(tmp), std::end(tmp), std::back_inserter(merged)); + XMLDoc->findObjects(ret, selector->mXPath, earlyReturn); - return merged; + return; } - return comparer.findObjects(root); + comparer.findObjects(ret, root); } -std::vector> Comparer::findObjects(const std::shared_ptr& root) +void Comparer::findObjects(std::vector> &ret, + const std::shared_ptr& root) { std::list> partialList{}; - std::vector> ret = findObjects(root, 0, 1, partialList); + findObjects(ret, root, 0, 1, partialList); LOGI("%d object(s) found", (int)ret.size()); - return ret; } -std::vector> Comparer::findObjects( +void Comparer::findObjects(std::vector> &ret, const std::shared_ptr& root, const int &index, const int &depth, std::list> &partialMatches) { - std::vector> ret; - if (mSelector->mMatchShowing && !root->isShowing()) return ret; + if (mSelector->mMatchShowing && !root->isShowing()) return; for (auto &match : partialMatches) match->update(root, index, depth, partialMatches); @@ -102,12 +102,10 @@ std::vector> Comparer::findObjects( auto child = children[i]; if (child->getRawHandler() == nullptr) continue; - std::vector> childret = - findObjects(child, i, depth + 1, partialMatches); - std::move(std::begin(childret), std::end(childret), std::back_inserter(ret)); + findObjects(ret, child, i, depth + 1, partialMatches); if (!ret.empty() && mEarlyReturn) { LOGI("Object found and earlyReturn"); - return ret; + return; } } } else { @@ -118,6 +116,4 @@ std::vector> Comparer::findObjects( LOGI("Found matched = %s with criteria %s", root->description().c_str(), currentMatch->debugPrint().c_str()); ret.push_back(root); } - - return ret; } diff --git a/libaurum/src/UiDevice.cc b/libaurum/src/UiDevice.cc index 042837b..e6e24fe 100644 --- a/libaurum/src/UiDevice.cc +++ b/libaurum/src/UiDevice.cc @@ -258,8 +258,9 @@ std::vector> UiDevice::findObjects( std::vector> ret{}; auto rootNodes = getWindowRoot(); for (const auto &window : rootNodes) { - std::vector> nodes = - Comparer::findObjects(getInstance(), selector, window); + std::vector> nodes{}; + Comparer::findObjects(nodes, getInstance(), selector, window); + for (auto &node : nodes) ret.push_back(std::make_shared(getInstance(), selector, node)); } diff --git a/libaurum/src/UiObject.cc b/libaurum/src/UiObject.cc index 41ef208..3bbecc0 100644 --- a/libaurum/src/UiObject.cc +++ b/libaurum/src/UiObject.cc @@ -92,7 +92,9 @@ std::vector> UiObject::findObjects( const std::shared_ptr selector) const { std::vector> result{}; - auto nodes = Comparer::findObjects(mDevice, selector, getAccessibleNode()); + + std::vector> nodes{}; + Comparer::findObjects(nodes, mDevice, selector, getAccessibleNode()); for ( auto& node : nodes) { if (!node) { LOGI("Skipped! (node == nullptr)"); diff --git a/org.tizen.aurum-bootstrap/src/Commands/FindElementsCommand.cc b/org.tizen.aurum-bootstrap/src/Commands/FindElementsCommand.cc index e0575cd..626d6a9 100644 --- a/org.tizen.aurum-bootstrap/src/Commands/FindElementsCommand.cc +++ b/org.tizen.aurum-bootstrap/src/Commands/FindElementsCommand.cc @@ -81,7 +81,6 @@ std::vector> FindElementsCommand::getSelectors(void) ::grpc::Status FindElementsCommand::execute() { - LOGI("findElements --------------- "); auto searchableObj = getSearchableTop(); auto selectors = getSelectors(); std::shared_ptr mDevice = UiDevice::getInstance(); -- 2.7.4