Messages in this thread
Patch in this message
 From Michel Lespinasse <> Subject [PATCH 11/13] rbtree: optimize case selection logic in __rb_erase_color() Date Mon, 9 Jul 2012 16:35:21 -0700
`In __rb_erase_color(), we have to select one of 3 cases depending on thecolor on the 'other' node children. If both children are black, we flipa few node colors and iterate. Otherwise, we do either one or twotree rotations, depending on the color of the 'other' child oppositeto 'node', and then we are done.The corresponding logic had duplicate checks for the color of the 'other'child opposite to 'node'. It was checking it first to determine if bothchildren are black, and then to determine how many tree rotations arerequired. Rearrange the logic to avoid that extra check.Signed-off-by: Michel Lespinasse <walken@google.com>--- lib/rbtree.c |   68 +++++++++++++++++++++++++-------------------------------- 1 files changed, 30 insertions(+), 38 deletions(-)diff --git a/lib/rbtree.c b/lib/rbtree.cindex 44cbbd5..597c1b9 100644--- a/lib/rbtree.c+++ b/lib/rbtree.c@@ -233,28 +233,24 @@ static void __rb_erase_color(struct rb_node *node, struct rb_node *parent, 				__rb_rotate_left(parent, root); 				other = parent->rb_right; 			}-			if ((!other->rb_left || rb_is_black(other->rb_left)) &&-			    (!other->rb_right || rb_is_black(other->rb_right)))-			{-				rb_set_red(other);-				node = parent;-				parent = rb_parent(node);-			}-			else-			{-				if (!other->rb_right || rb_is_black(other->rb_right))-				{-					rb_set_black(other->rb_left);+			if (!other->rb_right || rb_is_black(other->rb_right)) {+				if (!other->rb_left ||+				    rb_is_black(other->rb_left)) { 					rb_set_red(other);-					__rb_rotate_right(other, root);-					other = parent->rb_right;+					node = parent;+					parent = rb_parent(node);+					continue; 				}-				rb_set_color(other, rb_color(parent));-				rb_set_black(parent);-				rb_set_black(other->rb_right);-				__rb_rotate_left(parent, root);-				break;+				rb_set_black(other->rb_left);+				rb_set_red(other);+				__rb_rotate_right(other, root);+				other = parent->rb_right; 			}+			rb_set_color(other, rb_color(parent));+			rb_set_black(parent);+			rb_set_black(other->rb_right);+			__rb_rotate_left(parent, root);+			break; 		} else { 			other = parent->rb_left; 			if (rb_is_red(other))@@ -264,28 +260,24 @@ static void __rb_erase_color(struct rb_node *node, struct rb_node *parent, 				__rb_rotate_right(parent, root); 				other = parent->rb_left; 			}-			if ((!other->rb_left || rb_is_black(other->rb_left)) &&-			    (!other->rb_right || rb_is_black(other->rb_right)))-			{-				rb_set_red(other);-				node = parent;-				parent = rb_parent(node);-			}-			else-			{-				if (!other->rb_left || rb_is_black(other->rb_left))-				{-					rb_set_black(other->rb_right);+			if (!other->rb_left || rb_is_black(other->rb_left)) {+				if (!other->rb_right ||+				    rb_is_black(other->rb_right)) { 					rb_set_red(other);-					__rb_rotate_left(other, root);-					other = parent->rb_left;+					node = parent;+					parent = rb_parent(node);+					continue; 				}-				rb_set_color(other, rb_color(parent));-				rb_set_black(parent);-				rb_set_black(other->rb_left);-				__rb_rotate_right(parent, root);-				break;+				rb_set_black(other->rb_right);+				rb_set_red(other);+				__rb_rotate_left(other, root);+				other = parent->rb_left; 			}+			rb_set_color(other, rb_color(parent));+			rb_set_black(parent);+			rb_set_black(other->rb_left);+			__rb_rotate_right(parent, root);+			break; 		} 	} }-- 1.7.7.3`

Last update: 2012-07-10 02:01    [from the cache]
©2003-2011 Jasper Spaans