aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMichel Lespinasse <walken@google.com>2012-10-08 19:30:42 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-10-09 03:22:33 -0400
commit1f0528653e41ec230c60f5738820e8a544731399 (patch)
treef07f4eb1ed58122b810b586839833e0c015b681c /lib
parent910a742d4ba863848c7283d69c21bfa779d3b9a8 (diff)
rbtree: break out of rb_insert_color loop after tree rotation
It is a well known property of rbtrees that insertion never requires more than two tree rotations. In our implementation, after one loop iteration identified one or two necessary tree rotations, we would iterate and look for more. However at that point the node's parent would always be black, which would cause us to exit the loop. We can make the code flow more obvious by just adding a break statement after the tree rotations, where we know we are done. Additionally, in the cases where two tree rotations are necessary, we don't have to update the 'node' pointer as it wouldn't be used until the next loop iteration, which we now avoid due to this break statement. Signed-off-by: Michel Lespinasse <walken@google.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Acked-by: David 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: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/rbtree.c14
1 files changed, 4 insertions, 10 deletions
diff --git a/lib/rbtree.c b/lib/rbtree.c
index ccada9abe6f5..12abb8abf442 100644
--- a/lib/rbtree.c
+++ b/lib/rbtree.c
@@ -109,18 +109,15 @@ void rb_insert_color(struct rb_node *node, struct rb_root *root)
109 } 109 }
110 } 110 }
111 111
112 if (parent->rb_right == node) 112 if (parent->rb_right == node) {
113 {
114 register struct rb_node *tmp;
115 __rb_rotate_left(parent, root); 113 __rb_rotate_left(parent, root);
116 tmp = parent;
117 parent = node; 114 parent = node;
118 node = tmp;
119 } 115 }
120 116
121 rb_set_black(parent); 117 rb_set_black(parent);
122 rb_set_red(gparent); 118 rb_set_red(gparent);
123 __rb_rotate_right(gparent, root); 119 __rb_rotate_right(gparent, root);
120 break;
124 } else { 121 } else {
125 { 122 {
126 register struct rb_node *uncle = gparent->rb_left; 123 register struct rb_node *uncle = gparent->rb_left;
@@ -134,18 +131,15 @@ void rb_insert_color(struct rb_node *node, struct rb_root *root)
134 } 131 }
135 } 132 }
136 133
137 if (parent->rb_left == node) 134 if (parent->rb_left == node) {
138 {
139 register struct rb_node *tmp;
140 __rb_rotate_right(parent, root); 135 __rb_rotate_right(parent, root);
141 tmp = parent;
142 parent = node; 136 parent = node;
143 node = tmp;
144 } 137 }
145 138
146 rb_set_black(parent); 139 rb_set_black(parent);
147 rb_set_red(gparent); 140 rb_set_red(gparent);
148 __rb_rotate_left(gparent, root); 141 __rb_rotate_left(gparent, root);
142 break;
149 } 143 }
150 } 144 }
151 145