]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-journal: reduce number of calls generic_array_bisect_plus_one()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 27 Sep 2023 19:15:04 +0000 (04:15 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 6 Oct 2023 00:55:04 +0000 (09:55 +0900)
If the first call of generic_array_bisect_plus_one() provides the same
offset, then it is not necessary to call the next one, as we already
know the entry object is also liked to the input data object.

Also, this make the function reuse the object returned by
generic_array_bisect_plus_one().

No functional change, just optimization.

src/libsystemd/sd-journal/journal-file.c

index 05bdac9ed4a998e007f78a40b0a38cab47a06384..750f05d80b5b113409a21f917211be408eaf5161 100644 (file)
@@ -3542,7 +3542,7 @@ int journal_file_move_to_entry_by_monotonic_for_data(
                 uint64_t *ret_offset) {
 
         uint64_t z, entry_offset, entry_array_offset, n_entries;
-        Object *o;
+        Object *o, *entry;
         int r;
 
         assert(f);
@@ -3570,10 +3570,12 @@ int journal_file_move_to_entry_by_monotonic_for_data(
         if (r <= 0)
                 return r;
 
-        /* And now, continue seeking until we find an entry that
-         * exists in both bisection arrays */
+        /* And now, continue seeking until we find an entry that exists in both bisection arrays. */
         for (;;) {
-                uint64_t p, q;
+                uint64_t p;
+
+                /* The journal entry found by the above bisect_plus_one() may not have the specified data,
+                 * that is, it may not be linked in the data object. So, we need to check that. */
 
                 r = generic_array_bisect_plus_one(f,
                                                   entry_offset,
@@ -3582,9 +3584,15 @@ int journal_file_move_to_entry_by_monotonic_for_data(
                                                   z,
                                                   test_object_offset,
                                                   direction,
-                                                  NULL, &p);
+                                                  ret_object ? &entry : NULL, &p);
                 if (r <= 0)
                         return r;
+                if (p == z)
+                        break; /* The journal entry has the specified data. Yay! */
+
+                /* If the entry does not have the data, then move to the next (or previous, depends on the
+                 * 'direction') entry linked to the data object. But, the next entry may be in another boot.
+                 * So, we need to check that the entry has the matching boot ID. */
 
                 r = generic_array_bisect_plus_one(f,
                                                   le64toh(o->data.entry_offset),
@@ -3593,26 +3601,20 @@ int journal_file_move_to_entry_by_monotonic_for_data(
                                                   p,
                                                   test_object_offset,
                                                   direction,
-                                                  NULL, &q);
-
+                                                  ret_object ? &entry : NULL, &z);
                 if (r <= 0)
                         return r;
+                if (p == z)
+                        break; /* The journal entry has the specified boot ID. Yay! */
 
-                if (p == q) {
-                        if (ret_object) {
-                                r = journal_file_move_to_object(f, OBJECT_ENTRY, q, ret_object);
-                                if (r < 0)
-                                        return r;
-                        }
-
-                        if (ret_offset)
-                                *ret_offset = q;
-
-                        return 1;
-                }
-
-                z = q;
+                /* If not, let's try to the next entry... */
         }
+
+        if (ret_object)
+                *ret_object = entry;
+        if (ret_offset)
+                *ret_offset = z;
+        return 1;
 }
 
 int journal_file_move_to_entry_by_seqnum_for_data(