Skip to contentSkip to conversation

maple_tree: simplify mas_wr_node_walk for improved readability

PATCH Superseded Hao Li 1 participant 2 messages Aug 23, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Hao Li

author
#0 lore raw

PATCH 1/1 maple_tree: simplify mas_wr_node_walk for improved readability

Refactor mas_wr_node_walk to make the code more clear and easier to understand. The main changes are:

1. Replace the forward-iterating loop with a backward-iterating loop. This simplifies the logic for determining the correct range containing mas->index.

2. Eliminate the ternary operator.

The new implementation maintains the same functionality as before, but with improved readability. The performance characteristics remain essentially the same, as we cannot predict which interval mas->index will fall into.

---
 lib/maple_tree.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
lib/maple_tree.c +7 -9
index fe1b01b29..a180f54f5 100644
@@ -2213,16 +2213,14 @@ static inline void mas_wr_node_walk(struct ma_wr_state *wr_mas)
 
 	wr_mas->node = mas_mn(wr_mas->mas);
 	wr_mas->pivots = ma_pivots(wr_mas->node, wr_mas->type);
-	count = mas->end = ma_data_end(wr_mas->node, wr_mas->type,
+	mas->end = ma_data_end(wr_mas->node, wr_mas->type,
 				       wr_mas->pivots, mas->max);
-	offset = mas->offset;
-
-	while (offset < count && mas->index > wr_mas->pivots[offset])
-		offset++;
-
-	wr_mas->r_max = offset < count ? wr_mas->pivots[offset] : mas->max;
-	wr_mas->r_min = mas_safe_min(mas, wr_mas->pivots, offset);
-	wr_mas->offset_end = mas->offset = offset;
+	wr_mas->r_max = mas->max;
+	idx = mas->end - 1;
+	while (idx >= mas->offset && wr_mas->pivots[idx] >= mas->index)
+		wr_mas->r_max = wr_mas->pivots[idx--];
+	wr_mas->offset_end = mas->offset = idx + 1;
+	wr_mas->r_min = mas_safe_min(mas, wr_mas->pivots, mas->offset);
 }
 
 /*
Signature · 1 line
2.21.0
author
#1 lore raw

Sorry, this patch misses a line. v2 has been sent.

Quoted 50 lines · Hao Li

Refactor mas_wr_node_walk to make the code more clear and easier to understand. The main changes are:

1. Replace the forward-iterating loop with a backward-iterating loop. This simplifies the logic for determining the correct range containing mas->index.

2. Eliminate the ternary operator.

The new implementation maintains the same functionality as before, but with improved readability. The performance characteristics remain essentially the same, as we cannot predict which interval mas->index will fall into.

---
 lib/maple_tree.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
lib/maple_tree.c +7 -9
index fe1b01b29..a180f54f5 100644
@@ -2213,16 +2213,14 @@ static inline void mas_wr_node_walk(struct ma_wr_state *wr_mas)
 
 	wr_mas->node = mas_mn(wr_mas->mas);
 	wr_mas->pivots = ma_pivots(wr_mas->node, wr_mas->type);
-	count = mas->end = ma_data_end(wr_mas->node, wr_mas->type,
+	mas->end = ma_data_end(wr_mas->node, wr_mas->type,
 				       wr_mas->pivots, mas->max);
-	offset = mas->offset;
-
-	while (offset < count && mas->index > wr_mas->pivots[offset])
-		offset++;
-
-	wr_mas->r_max = offset < count ? wr_mas->pivots[offset] : mas->max;
-	wr_mas->r_min = mas_safe_min(mas, wr_mas->pivots, offset);
-	wr_mas->offset_end = mas->offset = offset;
+	wr_mas->r_max = mas->max;
+	idx = mas->end - 1;
+	while (idx >= mas->offset && wr_mas->pivots[idx] >= mas->index)
+		wr_mas->r_max = wr_mas->pivots[idx--];
+	wr_mas->offset_end = mas->offset = idx + 1;
+	wr_mas->r_min = mas_safe_min(mas, wr_mas->pivots, mas->offset);
 }
 
 /*
Signature · 1 line
2.21.0