]> git.tue.mpg.de Git - paraslash.git/commitdiff
btr: Speed up btr_node_status().
authorAndre Noll <maan@tuebingen.mpg.de>
Fri, 30 Dec 2022 13:09:06 +0000 (14:09 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Wed, 8 May 2024 17:45:52 +0000 (19:45 +0200)
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%

buffer_tree.c

index 35353f56c9f69cb99be6bb6a319f680876d4a9cc..5d97f86fc4b7140c563d9a9b47ca680133a92946 100644 (file)
@@ -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);
 }
 
 /**