Skip to content

Commit

Permalink
rbtree: adjust root color in rb_insert_color() only when necessary
Browse files Browse the repository at this point in the history
The root node of an rbtree must always be black.  However,
rb_insert_color() only needs to maintain this invariant when it has been
broken - that is, when it exits the loop due to the current (red) node
being the root.  In all other cases (exiting after tree rotations, or
exiting due to an existing black parent) the invariant is already
satisfied, so there is no need to adjust the root node color.

Signed-off-by: Michel Lespinasse <[email protected]>
Cc: Andrea Arcangeli <[email protected]>
Acked-by: David Woodhouse <[email protected]>
Cc: Rik van Riel <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Daniel Santos <[email protected]>
Cc: Jens Axboe <[email protected]>
Cc: "Eric W. Biederman" <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
walken-google authored and torvalds committed Oct 9, 2012
1 parent 1f05286 commit 6d58452
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/rbtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,21 @@ void rb_insert_color(struct rb_node *node, struct rb_root *root)
{
struct rb_node *parent, *gparent;

while ((parent = rb_parent(node)) && rb_is_red(parent))
{
while (true) {
/*
* Loop invariant: node is red
*
* If there is a black parent, we are done.
* Otherwise, take some corrective action as we don't
* want a red root or two consecutive red nodes.
*/
parent = rb_parent(node);
if (!parent) {
rb_set_black(node);
break;
} else if (rb_is_black(parent))
break;

gparent = rb_parent(parent);

if (parent == gparent->rb_left)
Expand Down Expand Up @@ -142,8 +155,6 @@ void rb_insert_color(struct rb_node *node, struct rb_root *root)
break;
}
}

rb_set_black(root->rb_node);
}
EXPORT_SYMBOL(rb_insert_color);

Expand Down

0 comments on commit 6d58452

Please sign in to comment.