Commit e125d147 authored by Michel Lespinasse's avatar Michel Lespinasse Committed by Linus Torvalds

rbtree: optimize case selection logic in __rb_erase_color()

In __rb_erase_color(), we have to select one of 3 cases depending on the
color on the 'other' node children.  If both children are black, we flip a
few node colors and iterate.  Otherwise, we do either one or two tree
rotations, depending on the color of the 'other' child opposite to '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 both
children are black, and then to determine how many tree rotations are
required.  Rearrange the logic to avoid that extra check.
Signed-off-by: default avatarMichel Lespinasse <walken@google.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Acked-by: default avatarDavid Woodhouse <David.Woodhouse@intel.com>
Cc: Rik van Riel <riel@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Daniel Santos <daniel.santos@pobox.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent d6ff1273
......@@ -283,17 +283,14 @@ 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)))
{
if (!other->rb_right || rb_is_black(other->rb_right)) {
if (!other->rb_left ||
rb_is_black(other->rb_left)) {
rb_set_red(other);
node = parent;
parent = rb_parent(node);
continue;
}
else
{
if (!other->rb_right || rb_is_black(other->rb_right))
{
rb_set_black(other->rb_left);
rb_set_red(other);
__rb_rotate_right(other, root);
......@@ -304,7 +301,6 @@ static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
rb_set_black(other->rb_right);
__rb_rotate_left(parent, root);
break;
}
} else {
other = parent->rb_left;
if (rb_is_red(other))
......@@ -314,17 +310,14 @@ 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)))
{
if (!other->rb_left || rb_is_black(other->rb_left)) {
if (!other->rb_right ||
rb_is_black(other->rb_right)) {
rb_set_red(other);
node = parent;
parent = rb_parent(node);
continue;
}
else
{
if (!other->rb_left || rb_is_black(other->rb_left))
{
rb_set_black(other->rb_right);
rb_set_red(other);
__rb_rotate_left(other, root);
......@@ -337,7 +330,6 @@ static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
break;
}
}
}
}
void rb_erase(struct rb_node *node, struct rb_root *root)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment