From 80f96c0ce1510d1ad4f8e0699d1fbc8958777812 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 28 Sep 2023 04:15:04 +0900 Subject: [PATCH] sd-journal: reduce number of calls generic_array_bisect_plus_one() 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 | 44 +++++++++++++----------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c index 05bdac9ed4a..750f05d80b5 100644 --- a/src/libsystemd/sd-journal/journal-file.c +++ b/src/libsystemd/sd-journal/journal-file.c @@ -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(