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

rbtree: avoid generating code twice for the cached versions (tools copy)

As was already noted in rbtree.h, the logic to cache rb_first (or
rb_last) can easily be implemented externally to the core rbtree api.

This commit takes the changes applied to the include/linux/ and lib/
rbtree files in 9f973cb3 ("lib/rbtree: avoid generating code twice
for the cached versions"), and applies these to the
tools/include/linux/ and tools/lib/ files as well to keep them
synchronized.

Link: http://lkml.kernel.org/r/20190703034812.53002-1-walken@google.comSigned-off-by: default avatarMichel Lespinasse <walken@google.com>
Cc: David Howells <dhowells@redhat.com>
Cc: Davidlohr Bueso <dbueso@suse.de>
Cc: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 0f749140
...@@ -31,25 +31,9 @@ struct rb_root { ...@@ -31,25 +31,9 @@ struct rb_root {
struct rb_node *rb_node; struct rb_node *rb_node;
}; };
/*
* Leftmost-cached rbtrees.
*
* We do not cache the rightmost node based on footprint
* size vs number of potential users that could benefit
* from O(1) rb_last(). Just not worth it, users that want
* this feature can always implement the logic explicitly.
* Furthermore, users that want to cache both pointers may
* find it a bit asymmetric, but that's ok.
*/
struct rb_root_cached {
struct rb_root rb_root;
struct rb_node *rb_leftmost;
};
#define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3)) #define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3))
#define RB_ROOT (struct rb_root) { NULL, } #define RB_ROOT (struct rb_root) { NULL, }
#define RB_ROOT_CACHED (struct rb_root_cached) { {NULL, }, NULL }
#define rb_entry(ptr, type, member) container_of(ptr, type, member) #define rb_entry(ptr, type, member) container_of(ptr, type, member)
#define RB_EMPTY_ROOT(root) (READ_ONCE((root)->rb_node) == NULL) #define RB_EMPTY_ROOT(root) (READ_ONCE((root)->rb_node) == NULL)
...@@ -71,12 +55,6 @@ extern struct rb_node *rb_prev(const struct rb_node *); ...@@ -71,12 +55,6 @@ extern struct rb_node *rb_prev(const struct rb_node *);
extern struct rb_node *rb_first(const struct rb_root *); extern struct rb_node *rb_first(const struct rb_root *);
extern struct rb_node *rb_last(const struct rb_root *); extern struct rb_node *rb_last(const struct rb_root *);
extern void rb_insert_color_cached(struct rb_node *,
struct rb_root_cached *, bool);
extern void rb_erase_cached(struct rb_node *node, struct rb_root_cached *);
/* Same as rb_first(), but O(1) */
#define rb_first_cached(root) (root)->rb_leftmost
/* Postorder iteration - always visit the parent after its children */ /* Postorder iteration - always visit the parent after its children */
extern struct rb_node *rb_first_postorder(const struct rb_root *); extern struct rb_node *rb_first_postorder(const struct rb_root *);
extern struct rb_node *rb_next_postorder(const struct rb_node *); extern struct rb_node *rb_next_postorder(const struct rb_node *);
...@@ -84,8 +62,6 @@ extern struct rb_node *rb_next_postorder(const struct rb_node *); ...@@ -84,8 +62,6 @@ extern struct rb_node *rb_next_postorder(const struct rb_node *);
/* Fast replacement of a single node without remove/rebalance/add/rebalance */ /* Fast replacement of a single node without remove/rebalance/add/rebalance */
extern void rb_replace_node(struct rb_node *victim, struct rb_node *new, extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
struct rb_root *root); struct rb_root *root);
extern void rb_replace_node_cached(struct rb_node *victim, struct rb_node *new,
struct rb_root_cached *root);
static inline void rb_link_node(struct rb_node *node, struct rb_node *parent, static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
struct rb_node **rb_link) struct rb_node **rb_link)
...@@ -129,4 +105,51 @@ static inline void rb_erase_init(struct rb_node *n, struct rb_root *root) ...@@ -129,4 +105,51 @@ static inline void rb_erase_init(struct rb_node *n, struct rb_root *root)
rb_erase(n, root); rb_erase(n, root);
RB_CLEAR_NODE(n); RB_CLEAR_NODE(n);
} }
/*
* Leftmost-cached rbtrees.
*
* We do not cache the rightmost node based on footprint
* size vs number of potential users that could benefit
* from O(1) rb_last(). Just not worth it, users that want
* this feature can always implement the logic explicitly.
* Furthermore, users that want to cache both pointers may
* find it a bit asymmetric, but that's ok.
*/
struct rb_root_cached {
struct rb_root rb_root;
struct rb_node *rb_leftmost;
};
#define RB_ROOT_CACHED (struct rb_root_cached) { {NULL, }, NULL }
/* Same as rb_first(), but O(1) */
#define rb_first_cached(root) (root)->rb_leftmost
static inline void rb_insert_color_cached(struct rb_node *node,
struct rb_root_cached *root,
bool leftmost)
{
if (leftmost)
root->rb_leftmost = node;
rb_insert_color(node, &root->rb_root);
}
static inline void rb_erase_cached(struct rb_node *node,
struct rb_root_cached *root)
{
if (root->rb_leftmost == node)
root->rb_leftmost = rb_next(node);
rb_erase(node, &root->rb_root);
}
static inline void rb_replace_node_cached(struct rb_node *victim,
struct rb_node *new,
struct rb_root_cached *root)
{
if (root->rb_leftmost == victim)
root->rb_leftmost = new;
rb_replace_node(victim, new, &root->rb_root);
}
#endif /* __TOOLS_LINUX_PERF_RBTREE_H */ #endif /* __TOOLS_LINUX_PERF_RBTREE_H */
...@@ -32,17 +32,16 @@ struct rb_augment_callbacks { ...@@ -32,17 +32,16 @@ struct rb_augment_callbacks {
void (*rotate)(struct rb_node *old, struct rb_node *new); void (*rotate)(struct rb_node *old, struct rb_node *new);
}; };
extern void __rb_insert_augmented(struct rb_node *node, extern void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
struct rb_root *root,
bool newleft, struct rb_node **leftmost,
void (*augment_rotate)(struct rb_node *old, struct rb_node *new)); void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
/* /*
* Fixup the rbtree and update the augmented information when rebalancing. * Fixup the rbtree and update the augmented information when rebalancing.
* *
* On insertion, the user must update the augmented information on the path * On insertion, the user must update the augmented information on the path
* leading to the inserted node, then call rb_link_node() as usual and * leading to the inserted node, then call rb_link_node() as usual and
* rb_augment_inserted() instead of the usual rb_insert_color() call. * rb_insert_augmented() instead of the usual rb_insert_color() call.
* If rb_augment_inserted() rebalances the rbtree, it will callback into * If rb_insert_augmented() rebalances the rbtree, it will callback into
* a user provided function to update the augmented information on the * a user provided function to update the augmented information on the
* affected subtrees. * affected subtrees.
*/ */
...@@ -50,7 +49,7 @@ static inline void ...@@ -50,7 +49,7 @@ static inline void
rb_insert_augmented(struct rb_node *node, struct rb_root *root, rb_insert_augmented(struct rb_node *node, struct rb_root *root,
const struct rb_augment_callbacks *augment) const struct rb_augment_callbacks *augment)
{ {
__rb_insert_augmented(node, root, false, NULL, augment->rotate); __rb_insert_augmented(node, root, augment->rotate);
} }
static inline void static inline void
...@@ -58,8 +57,9 @@ rb_insert_augmented_cached(struct rb_node *node, ...@@ -58,8 +57,9 @@ rb_insert_augmented_cached(struct rb_node *node,
struct rb_root_cached *root, bool newleft, struct rb_root_cached *root, bool newleft,
const struct rb_augment_callbacks *augment) const struct rb_augment_callbacks *augment)
{ {
__rb_insert_augmented(node, &root->rb_root, if (newleft)
newleft, &root->rb_leftmost, augment->rotate); root->rb_leftmost = node;
rb_insert_augmented(node, &root->rb_root, augment);
} }
#define RB_DECLARE_CALLBACKS(rbstatic, rbname, rbstruct, rbfield, \ #define RB_DECLARE_CALLBACKS(rbstatic, rbname, rbstruct, rbfield, \
...@@ -139,7 +139,6 @@ extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root, ...@@ -139,7 +139,6 @@ extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
static __always_inline struct rb_node * static __always_inline struct rb_node *
__rb_erase_augmented(struct rb_node *node, struct rb_root *root, __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
struct rb_node **leftmost,
const struct rb_augment_callbacks *augment) const struct rb_augment_callbacks *augment)
{ {
struct rb_node *child = node->rb_right; struct rb_node *child = node->rb_right;
...@@ -147,9 +146,6 @@ __rb_erase_augmented(struct rb_node *node, struct rb_root *root, ...@@ -147,9 +146,6 @@ __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
struct rb_node *parent, *rebalance; struct rb_node *parent, *rebalance;
unsigned long pc; unsigned long pc;
if (leftmost && node == *leftmost)
*leftmost = rb_next(node);
if (!tmp) { if (!tmp) {
/* /*
* Case 1: node to erase has no more than 1 child (easy!) * Case 1: node to erase has no more than 1 child (easy!)
...@@ -249,8 +245,7 @@ static __always_inline void ...@@ -249,8 +245,7 @@ static __always_inline void
rb_erase_augmented(struct rb_node *node, struct rb_root *root, rb_erase_augmented(struct rb_node *node, struct rb_root *root,
const struct rb_augment_callbacks *augment) const struct rb_augment_callbacks *augment)
{ {
struct rb_node *rebalance = __rb_erase_augmented(node, root, struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
NULL, augment);
if (rebalance) if (rebalance)
__rb_erase_color(rebalance, root, augment->rotate); __rb_erase_color(rebalance, root, augment->rotate);
} }
...@@ -259,11 +254,9 @@ static __always_inline void ...@@ -259,11 +254,9 @@ static __always_inline void
rb_erase_augmented_cached(struct rb_node *node, struct rb_root_cached *root, rb_erase_augmented_cached(struct rb_node *node, struct rb_root_cached *root,
const struct rb_augment_callbacks *augment) const struct rb_augment_callbacks *augment)
{ {
struct rb_node *rebalance = __rb_erase_augmented(node, &root->rb_root, if (root->rb_leftmost == node)
&root->rb_leftmost, root->rb_leftmost = rb_next(node);
augment); rb_erase_augmented(node, &root->rb_root, augment);
if (rebalance)
__rb_erase_color(rebalance, &root->rb_root, augment->rotate);
} }
#endif /* _TOOLS_LINUX_RBTREE_AUGMENTED_H */ #endif /* _TOOLS_LINUX_RBTREE_AUGMENTED_H */
...@@ -83,14 +83,10 @@ __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new, ...@@ -83,14 +83,10 @@ __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
static __always_inline void static __always_inline void
__rb_insert(struct rb_node *node, struct rb_root *root, __rb_insert(struct rb_node *node, struct rb_root *root,
bool newleft, struct rb_node **leftmost,
void (*augment_rotate)(struct rb_node *old, struct rb_node *new)) void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
{ {
struct rb_node *parent = rb_red_parent(node), *gparent, *tmp; struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
if (newleft)
*leftmost = node;
while (true) { while (true) {
/* /*
* Loop invariant: node is red. * Loop invariant: node is red.
...@@ -436,34 +432,17 @@ static const struct rb_augment_callbacks dummy_callbacks = { ...@@ -436,34 +432,17 @@ static const struct rb_augment_callbacks dummy_callbacks = {
void rb_insert_color(struct rb_node *node, struct rb_root *root) void rb_insert_color(struct rb_node *node, struct rb_root *root)
{ {
__rb_insert(node, root, false, NULL, dummy_rotate); __rb_insert(node, root, dummy_rotate);
} }
void rb_erase(struct rb_node *node, struct rb_root *root) void rb_erase(struct rb_node *node, struct rb_root *root)
{ {
struct rb_node *rebalance; struct rb_node *rebalance;
rebalance = __rb_erase_augmented(node, root, rebalance = __rb_erase_augmented(node, root, &dummy_callbacks);
NULL, &dummy_callbacks);
if (rebalance) if (rebalance)
____rb_erase_color(rebalance, root, dummy_rotate); ____rb_erase_color(rebalance, root, dummy_rotate);
} }
void rb_insert_color_cached(struct rb_node *node,
struct rb_root_cached *root, bool leftmost)
{
__rb_insert(node, &root->rb_root, leftmost,
&root->rb_leftmost, dummy_rotate);
}
void rb_erase_cached(struct rb_node *node, struct rb_root_cached *root)
{
struct rb_node *rebalance;
rebalance = __rb_erase_augmented(node, &root->rb_root,
&root->rb_leftmost, &dummy_callbacks);
if (rebalance)
____rb_erase_color(rebalance, &root->rb_root, dummy_rotate);
}
/* /*
* Augmented rbtree manipulation functions. * Augmented rbtree manipulation functions.
* *
...@@ -472,10 +451,9 @@ void rb_erase_cached(struct rb_node *node, struct rb_root_cached *root) ...@@ -472,10 +451,9 @@ void rb_erase_cached(struct rb_node *node, struct rb_root_cached *root)
*/ */
void __rb_insert_augmented(struct rb_node *node, struct rb_root *root, void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
bool newleft, struct rb_node **leftmost,
void (*augment_rotate)(struct rb_node *old, struct rb_node *new)) void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
{ {
__rb_insert(node, root, newleft, leftmost, augment_rotate); __rb_insert(node, root, augment_rotate);
} }
/* /*
...@@ -580,15 +558,6 @@ void rb_replace_node(struct rb_node *victim, struct rb_node *new, ...@@ -580,15 +558,6 @@ void rb_replace_node(struct rb_node *victim, struct rb_node *new,
__rb_change_child(victim, new, parent, root); __rb_change_child(victim, new, parent, root);
} }
void rb_replace_node_cached(struct rb_node *victim, struct rb_node *new,
struct rb_root_cached *root)
{
rb_replace_node(victim, new, &root->rb_root);
if (root->rb_leftmost == victim)
root->rb_leftmost = new;
}
static struct rb_node *rb_left_deepest_node(const struct rb_node *node) static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
{ {
for (;;) { for (;;) {
......
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