[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20241007152833.2282199-5-visitorckw@gmail.com>
Date: Mon, 7 Oct 2024 23:28:31 +0800
From: Kuan-Wei Chiu <visitorckw@...il.com>
To: xavier_qy@....com,
longman@...hat.com,
lizefan.x@...edance.com,
tj@...nel.org,
hannes@...xchg.org,
mkoutny@...e.com,
akpm@...ux-foundation.org
Cc: jserv@...s.ncku.edu.tw,
linux-kernel@...r.kernel.org,
cgroups@...r.kernel.org,
Kuan-Wei Chiu <visitorckw@...il.com>
Subject: [PATCH v2 4/6] lib/union_find: Optimize uf_find() with enhanced path compression
Optimize the uf_find() function to enhance its efficiency by
implementing a more effective path compression strategy. The original
implementation only updated the parent pointer of the current node to
its grandparent, resulting in a relatively shallow tree.
In the updated version, once the root of the node is identified, all
nodes along the search path are updated to directly point to the root.
This change minimizes the height of the tree and improves the
efficiency for subsequent find operations, providing better performance
for the union-find data structure.
Signed-off-by: Kuan-Wei Chiu <visitorckw@...il.com>
---
Note: Tested with the KUnit tests introduced in the previous patch.
lib/union_find.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/lib/union_find.c b/lib/union_find.c
index a20678da0220..d2b79065cbc8 100644
--- a/lib/union_find.c
+++ b/lib/union_find.c
@@ -13,14 +13,19 @@
*/
struct uf_node *uf_find(struct uf_node *node)
{
+ struct uf_node *root = node;
struct uf_node *parent;
- while (node->parent != node) {
+ while (root->parent != root)
+ root = root->parent;
+
+ while (node->parent != root) {
parent = node->parent;
- node->parent = parent->parent;
+ node->parent = root;
node = parent;
}
- return node;
+
+ return root;
}
EXPORT_SYMBOL(uf_find);
--
2.34.1
Powered by blists - more mailing lists