]> git.kaiwu.me - njs.git/commitdiff
A small rbtree insert fixup optimization.
authorIgor Sysoev <igor@sysoev.ru>
Fri, 26 May 2017 17:10:22 +0000 (20:10 +0300)
committerIgor Sysoev <igor@sysoev.ru>
Fri, 26 May 2017 17:10:22 +0000 (20:10 +0300)
Thanks to 洪志道 (Hong Zhi Dao).

nxt/nxt_rbtree.c

index 18e8acca08fac33bba4319ec0bffcbdfba7fe179..6b5cd1be06fa2e02075026121ccf05f59b36c045 100644 (file)
@@ -129,11 +129,15 @@ nxt_rbtree_insert_fixup(nxt_rbtree_node_t *node)
                     nxt_rbtree_left_rotate(node);
                 }
 
+                /*
+                 * nxt_rbtree_left_rotate() swaps parent and
+                 * child whilst keeps grandparent the same.
+                 */
                 parent = node->parent;
-                parent->color = NXT_RBTREE_BLACK;
 
-                grandparent = parent->parent;
+                parent->color = NXT_RBTREE_BLACK;
                 grandparent->color = NXT_RBTREE_RED;
+
                 nxt_rbtree_right_rotate(grandparent);
                 /*
                  * nxt_rbtree_right_rotate() does not change node->parent
@@ -153,11 +157,12 @@ nxt_rbtree_insert_fixup(nxt_rbtree_node_t *node)
                     nxt_rbtree_right_rotate(node);
                 }
 
+                /* See the comment in the symmetric branch above. */
                 parent = node->parent;
-                parent->color = NXT_RBTREE_BLACK;
 
-                grandparent = parent->parent;
+                parent->color = NXT_RBTREE_BLACK;
                 grandparent->color = NXT_RBTREE_RED;
+
                 nxt_rbtree_left_rotate(grandparent);
 
                 /* See the comment in the symmetric branch above. */