Skip to contentSkip to conversation

rbtree: rb_erase updates and comments

PATCH Michel Lespinasse 2 participants 2 messages Jul 29, 2012 1 message not in the archive Memory management

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 11 lines · George Spelvin

I was just looking at the beginning of the 2-children case and wondering:

(quoted diff) +5 -0
+               /*
+                * Old is the node we want to erase. It's got left and right
+                * children, which makes things difficult. Let's find the
+                * next node in the tree to have it fill old's position.
+                */
                node = node->rb_right;

Er... isn't this already available in "child"? Why fetch it again (or make the compiler figure out that it doesn't have to)?

Good catch, I just failed to notice it myself :)

Quoted 3 lines

Another thing you can use for comments is that if a node has a single child, that child must be red. That can simplify the rb_set_parent code, since it does not need to read the old value.

Nicely observed, I hadn't thought of that one either.

Quoted 3 lines

Then the end of case 3 of rb_erase becomes:

        if (child)
                rb_set_parent_color(child, parent, RB_RED);

Yes. it's actually even nicer, because we know since the child is red, the node being erased is black, and we can thus handle recoloring 'for free' by setting child to black here instead of going through __rb_erase_color() later. And if we could do that for all 1-child cases, it might even be possible to invoke __rb_erase_color() for the no-childs case only, at which point we can drop one of that function's arguments. Worth investigating, I think.

Quoted 19 lines

A common idiom I see in the code:

(quoted diff) +7 -0
+               if (parent) {
+                       if (parent->rb_left == node)
+                               parent->rb_left = child;
+                       else
+                               parent->rb_right = child;
+               } else
+                       root->rb_node = child;

might be written more attractively as:

                if (unlikely(!parent))
                        root->rb_node = child;
                else if (parent->rb_left == node)
                        parent->rb_left = child;
                else
                        parent->rb_right = child;
Quoted 3 lines

I'm almost tempted to wrap that up in a helper function, although the lack of an obviously correct order for the three "struct rb_node *" parameters suggests that it would maybe be too confusing.

Using a helper doesn't hurt, I think. I'm not sure about the unlikely thing because near-empty trees could be common for some workloads, which is why I've let it the way it currently is.

Quoted 5 lines

Finally, it would be interesting to look at Sedgewick's left-leaning RB-tree to see if it could improve things. I'm not sure if it would, since in a multiprocessor system, the most important thing is minimizing cache line bouncing, which means minimizing writes, and he gets his code simplicity by tightening invariants, which means more write traffic.

Yeah, I've had a quick look at left-leaning RBtrees, but they didn't seem like an obvious win to me. Plus, I feel like I've been thinking about rbtrees too much already, so I kinda want to take a vacation from them at this point :)

Thanks for your remarks, especially the one about one-child coloring in rb_erase().

Signature · 8 lines
Michel "Walken" Lespinasse
A program is never fully debugged until the last user dies.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw
Quoted 11 lines
Quoted 3 lines

Then the end of case 3 of rb_erase becomes:

        if (child)
                rb_set_parent_color(child, parent, RB_RED);

Yes. it's actually even nicer, because we know since the child is red, the node being erased is black, and we can thus handle recoloring 'for free' by setting child to black here instead of going through __rb_erase_color() later. And if we could do that for all 1-child cases, it might even be possible to invoke __rb_erase_color() for the no-childs case only, at which point we can drop one of that function's arguments. Worth investigating, I think.

I haven't wrapped my head around the recoloring functions yet. I stopped when I realized that if the child is red, the parent must be black, but the child might be NULL, in which case the parent could be any color.

But... oh! I see! It's not the node being erased, but the node being grafted. If it has a red child, then it's part of a virtual 3-node, and that can be downgraded to a 2-node without requiring any tree rebalancing!

Here's an (untested!) attempt at writing the code. Be aware that although I use the same variable names, there are significnt differences in what they're used for! In particular, "node" is never chnaged, so there's no need for "old", and the successor is "child".

The main simplifcation I did was rename "black" to "rebalance", initially set false, and only set "rebalance = rb_is_black(node)" when necessary.

Given the new variable uses, you could merge the "rebalance" and "parent" variables, so rebalance = false is implied by parent = NULL. I'm not sure the variable saving is worth the code complexity, though.

void rb_erase(struct rb_node * const node, struct rb_root * const root)

{
	struct rb_node *parent;
	struct rb_node *child = node->rb_right;
	struct rb_node *tmp = node->rb_left;
	bool rebalance = false;

	if (!tmp) {
		/* Case 1: node to erase has no more than 1 child (easy!) */
		if (!child) {
			/* Node might be black */
			rebalance = rb_is_black(node);
		} else {
one_child:		/*
			 * Node has a child: it must be black, and the child
			 * must be red.  So promote the child to black, and
			 * no rebalancing needed.
			 */
			child->__rb_parent_color = node->__rb_parent_color;
		}
		parent = tmp = rb_parent(node);	// Hope compiler can CSE
	} else if (!child) {
		/* Still case 1, but this time the child is node->rb_left */
		child = tmp;
		goto one_child;
	} else {
		/*
		 * The node we want to erase has both left and right
		 * children, which makes things difficult.  Let's find the
		 * next node in the tree to have it fill old's position.
		 */
		tmp = child->rb_left;
		if (!tmp) {
			/*
			 * Case 2: Successor is node's right child.
			 *
			 *    (n)          (c)
			 *    / \          / \
			 *  (x) (c)  ->  (x) (z)
			 *        \
			 *        (z)
			 *
			 * Node z may or may not exist.  If it exists, it must
			 * be red, and can be promoted to black with no other
			 * tree rebalancing.
			 */
			tmp = child->rb_right;
			parent = child;
		} else {
			/*
			 * Case 3: Successor is not direct child; it
			 * is the left child of a chain (y) of other
			 * nodes.
			 *
			 *    (n)          (c)
			 *    / \          / \
			 *  (x) (y)  ->  (x) (y)
			 *      /            /
			 *    (c)          (z)
			 *      \
			 *      (z)
			 *
			 * As above, z may or may not exist.
			 */
			do {
				parent = child;
				child = tmp;
				tmp = child->rb_left;
			} while (tmp);

			child->rb_right = tmp = node->rb_right;
			rb_set_parent(tmp, child);
			parent->rb_left = tmp = child->rb_right;
		}
		// Variable usage:
		// "parent" is the bottom of "(y)", which is the same as
		//	"child" in case 2.
		// "tmp" is "(z)"
		// child->rb_right has been set correctly, as has
		// the pointer to (z).
		/*
		 * Here there are two cases, just like in case 1.
		 * If child had a right child, then child was black
		 * and its child was red, and can be promoted with no
		 * rebalancing.  If it did not, child might be either
		 * color, and its parent rebalancing if it was black.
		 */
		if (tmp)
			// tmp->__rb_parent_color = child->__rb_parent_color;
			rb_set_parent_color(tmp, parent, RB_BLACK);
		else
			rebalance = rb_is_black(child);

		child->rb_left = node->rb_left;
		child->__rb_parent_color = node->__rb_parent_color;
		tmp = rb_parent(node);	// Hope compiler can CSE
	}
	if (!tmp)
		root->rb_node = child;
	else if (tmp->rb_left == node)
		tmp->rb_left = child;
	else
		tmp->rb_right = child;
	if (rebalance)
		__rb_erase_color(NULL, parent, root);
}
Quoted 3 lines

Using a helper doesn't hurt, I think. I'm not sure about the unlikely thing because near-empty trees could be common for some workloads, which is why I've let it the way it currently is.

I wasn't positive, either, but I figured it was easy enough to delete if you disagreed.

I did think of one possible simplification: return a pointer to the parent's child pointer and update that:

	*rb_pptr(old, parent, root) = new;

where

static inline struct rb_node **rb_pptr(struct rb_node *rb, struct rb_node *p,
struct rb_root *root)

{
	if (!p)
		return &root->rb_node;
	else if (parent->rb_left == node)
		return &parent->rb_left;
	else
		return &parent->rb_right;
}
Quoted 4 lines

Yeah, I've had a quick look at left-leaning RBtrees, but they didn't seem like an obvious win to me. Plus, I feel like I've been thinking about rbtrees too much already, so I kinda want to take a vacation from them at this point :)

Ah. Then I'll stop muttering about ideas that imply major rewrites.

Quoted 2 lines

Thanks for your remarks, especially the one about one-child coloring in rb_erase().

You're the one who spotted the major simplification, but I'm glad to have been of some minor help.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>