From: Andre Noll Date: Fri, 30 Dec 2022 13:09:06 +0000 (+0100) Subject: btr: Speed up btr_node_status(). X-Git-Url: http://git.tue.mpg.de/?a=commitdiff_plain;h=db654ceb7ced70cdd133c547196b3de4207e3112;p=paraslash.git btr: Speed up btr_node_status(). Currently we sum up the sizes of all buffers in the input queue just to determine if the total size exceeds a small threshold. That's silly and expensive if there are many buffers. Fix that by introducing a helper which breaks out of the loop as soon as know the answer because the threshold is exceeded. User time: 150s -> 147s, speedup: 2% --- diff --git a/buffer_tree.c b/buffer_tree.c index 35353f56..5d97f86f 100644 --- a/buffer_tree.c +++ b/buffer_tree.c @@ -833,6 +833,22 @@ size_t btr_get_input_queue_size(struct btr_node *btrn) return size; } +static bool min_iqs_available(size_t min_iqs, struct btr_node *btrn) +{ + struct btr_buffer_reference *br; + size_t have = 0, wrap_consumed = 0; + + FOR_EACH_BUFFER_REF(br, btrn) { + if (br->wrap_count != 0) { + wrap_consumed = br->consumed; + continue; + } + have += br_available_bytes(br); + if (have > wrap_consumed + min_iqs) + return true; + } + return false; +} /** * Remove a node from the buffer tree, reconnecting parent and children. * @@ -1202,9 +1218,6 @@ struct btr_node *btr_search_node(const char *name, struct btr_node *root) int btr_node_status(struct btr_node *btrn, size_t min_iqs, enum btr_node_type type) { - size_t iqs; - - assert(btrn); if (type != BTR_NT_LEAF && btr_no_children(btrn)) return -E_BTR_NO_CHILD; if (type != BTR_NT_ROOT && btr_eof(btrn)) @@ -1214,12 +1227,9 @@ int btr_node_status(struct btr_node *btrn, size_t min_iqs, return 0; if (type == BTR_NT_ROOT) return 1; - iqs = btr_get_input_queue_size(btrn); - if (iqs == 0) /* we have a parent, because not eof */ - return 0; - if (iqs < min_iqs && !btr_no_parent(btrn)) - return 0; - return 1; + if (min_iqs_available(min_iqs, btrn)) + return 1; + return btr_no_parent(btrn); } /**