|
108 | 108 | #include <sys/cmn_err.h>
|
109 | 109 | #include <sys/mod.h>
|
110 | 110 |
|
111 |
| -/* |
112 |
| - * Small arrays to translate between balance (or diff) values and child indices. |
113 |
| - * |
114 |
| - * Code that deals with binary tree data structures will randomly use |
115 |
| - * left and right children when examining a tree. C "if()" statements |
116 |
| - * which evaluate randomly suffer from very poor hardware branch prediction. |
117 |
| - * In this code we avoid some of the branch mispredictions by using the |
118 |
| - * following translation arrays. They replace random branches with an |
119 |
| - * additional memory reference. Since the translation arrays are both very |
120 |
| - * small the data should remain efficiently in cache. |
121 |
| - */ |
122 |
| -static const int avl_child2balance[2] = {-1, 1}; |
123 |
| -static const int avl_balance2child[] = {0, 0, 1}; |
124 |
| - |
125 |
| - |
126 | 111 | /*
|
127 | 112 | * Walk from one node to the previous valued node (ie. an infix walk
|
128 | 113 | * towards the left). At any given node we do one of 2 things:
|
@@ -278,8 +263,7 @@ avl_find(avl_tree_t *tree, const void *value, avl_index_t *where)
|
278 | 263 | #endif
|
279 | 264 | return (AVL_NODE2DATA(node, off));
|
280 | 265 | }
|
281 |
| - child = avl_balance2child[1 + diff]; |
282 |
| - |
| 266 | + child = (diff > 0); |
283 | 267 | }
|
284 | 268 |
|
285 | 269 | if (where != NULL)
|
@@ -531,7 +515,7 @@ avl_insert(avl_tree_t *tree, void *new_data, avl_index_t where)
|
531 | 515 | * Compute the new balance
|
532 | 516 | */
|
533 | 517 | old_balance = AVL_XBALANCE(node);
|
534 |
| - new_balance = old_balance + avl_child2balance[which_child]; |
| 518 | + new_balance = old_balance + (which_child ? 1 : -1); |
535 | 519 |
|
536 | 520 | /*
|
537 | 521 | * If we introduced equal balance, then we are done immediately
|
@@ -697,7 +681,7 @@ avl_remove(avl_tree_t *tree, void *data)
|
697 | 681 | * choose node to swap from whichever side is taller
|
698 | 682 | */
|
699 | 683 | old_balance = AVL_XBALANCE(delete);
|
700 |
| - left = avl_balance2child[old_balance + 1]; |
| 684 | + left = (old_balance > 0); |
701 | 685 | right = 1 - left;
|
702 | 686 |
|
703 | 687 | /*
|
@@ -781,7 +765,7 @@ avl_remove(avl_tree_t *tree, void *data)
|
781 | 765 | */
|
782 | 766 | node = parent;
|
783 | 767 | old_balance = AVL_XBALANCE(node);
|
784 |
| - new_balance = old_balance - avl_child2balance[which_child]; |
| 768 | + new_balance = old_balance - (which_child ? 1 : -1); |
785 | 769 | parent = AVL_XPARENT(node);
|
786 | 770 | which_child = AVL_XCHILD(node);
|
787 | 771 |
|
|
0 commit comments