From: Ken Gaillot <kgaillot@redhat.com>
Date: Thu, 2 Feb 2023 09:42:01 -0600
Subject: Refactor: scheduler: improve xpath efficiency when unpacking

Using "//" means that every child must be searched recursively. If we know the
exact path, we should explicitly specify it.
---
 lib/pengine/unpack.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/lib/pengine/unpack.c b/lib/pengine/unpack.c
index 5fcba3b..41cb980 100644
--- a/lib/pengine/unpack.c
+++ b/lib/pengine/unpack.c
@@ -2577,6 +2577,13 @@ set_node_score(gpointer key, gpointer value, gpointer user_data)
     node->weight = *score;
 }
 
+#define XPATH_NODE_STATE "/" XML_TAG_CIB "/" XML_CIB_TAG_STATUS     \
+                         "/" XML_CIB_TAG_STATE
+#define SUB_XPATH_LRM_RESOURCE "/" XML_CIB_TAG_LRM              \
+                               "/" XML_LRM_TAG_RESOURCES        \
+                               "/" XML_LRM_TAG_RESOURCE
+#define SUB_XPATH_LRM_RSC_OP "/" XML_LRM_TAG_RSC_OP
+
 static xmlNode *
 find_lrm_op(const char *resource, const char *op, const char *node, const char *source,
             int target_rc, pe_working_set_t *data_set)
@@ -2589,10 +2596,9 @@ find_lrm_op(const char *resource, const char *op, const char *node, const char *
 
     xpath = g_string_sized_new(256);
     pcmk__g_strcat(xpath,
-                   "//" XML_CIB_TAG_STATE "[@" XML_ATTR_UNAME "='", node, "']"
-                   "//" XML_LRM_TAG_RESOURCE
-                   "[@" XML_ATTR_ID "='", resource, "']"
-                   "/" XML_LRM_TAG_RSC_OP "[@" XML_LRM_ATTR_TASK "='", op, "'",
+                   XPATH_NODE_STATE "[@" XML_ATTR_UNAME "='", node, "']"
+                   SUB_XPATH_LRM_RESOURCE "[@" XML_ATTR_ID "='", resource, "']"
+                   SUB_XPATH_LRM_RSC_OP "[@" XML_LRM_ATTR_TASK "='", op, "'",
                    NULL);
 
     /* Need to check against transition_magic too? */
@@ -2637,10 +2643,8 @@ find_lrm_resource(const char *rsc_id, const char *node_name,
 
     xpath = g_string_sized_new(256);
     pcmk__g_strcat(xpath,
-                   "//" XML_CIB_TAG_STATE
-                   "[@" XML_ATTR_UNAME "='", node_name, "']"
-                   "//" XML_LRM_TAG_RESOURCE
-                   "[@" XML_ATTR_ID "='", rsc_id, "']",
+                   XPATH_NODE_STATE "[@" XML_ATTR_UNAME "='", node_name, "']"
+                   SUB_XPATH_LRM_RESOURCE "[@" XML_ATTR_ID "='", rsc_id, "']",
                    NULL);
 
     xml = get_xpath_object((const char *) xpath->str, data_set->input,
