9fc0f6
From 563bf9e9305aa88ad403a81c0c91418b7846f465 Mon Sep 17 00:00:00 2001
9fc0f6
From: Lennart Poettering <lennart@poettering.net>
9fc0f6
Date: Tue, 26 Nov 2013 20:37:53 +0100
9fc0f6
Subject: [PATCH] journal: optimize bisection logic a bit by caching the last
9fc0f6
 position
9fc0f6
9fc0f6
This way we can do a quick restart limiting a bit how wildly we need to
9fc0f6
jump around during the bisection process.
9fc0f6
---
9fc0f6
 src/journal/journal-file.c | 134 +++++++++++++++++++++++++++++++++------------
9fc0f6
 1 file changed, 99 insertions(+), 35 deletions(-)
9fc0f6
9fc0f6
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
9fc0f6
index 4062a83..7adb1b5 100644
9fc0f6
--- a/src/journal/journal-file.c
9fc0f6
+++ b/src/journal/journal-file.c
9fc0f6
@@ -1366,6 +1366,7 @@ typedef struct ChainCacheItem {
9fc0f6
         uint64_t array; /* the cached array */
9fc0f6
         uint64_t begin; /* the first item in the cached array */
9fc0f6
         uint64_t total; /* the total number of items in all arrays before this one in the chain */
9fc0f6
+        uint64_t last_index; /* the last index we looked at, to optimize locality when bisecting */
9fc0f6
 } ChainCacheItem;
9fc0f6
 
9fc0f6
 static void chain_cache_put(
9fc0f6
@@ -1374,7 +1375,8 @@ static void chain_cache_put(
9fc0f6
                 uint64_t first,
9fc0f6
                 uint64_t array,
9fc0f6
                 uint64_t begin,
9fc0f6
-                uint64_t total) {
9fc0f6
+                uint64_t total,
9fc0f6
+                uint64_t last_index) {
9fc0f6
 
9fc0f6
         if (!ci) {
9fc0f6
                 /* If the chain item to cache for this chain is the
9fc0f6
@@ -1402,12 +1404,14 @@ static void chain_cache_put(
9fc0f6
         ci->array = array;
9fc0f6
         ci->begin = begin;
9fc0f6
         ci->total = total;
9fc0f6
+        ci->last_index = last_index;
9fc0f6
 }
9fc0f6
 
9fc0f6
-static int generic_array_get(JournalFile *f,
9fc0f6
-                             uint64_t first,
9fc0f6
-                             uint64_t i,
9fc0f6
-                             Object **ret, uint64_t *offset) {
9fc0f6
+static int generic_array_get(
9fc0f6
+                JournalFile *f,
9fc0f6
+                uint64_t first,
9fc0f6
+                uint64_t i,
9fc0f6
+                Object **ret, uint64_t *offset) {
9fc0f6
 
9fc0f6
         Object *o;
9fc0f6
         uint64_t p = 0, a, t = 0;
9fc0f6
@@ -1448,7 +1452,7 @@ static int generic_array_get(JournalFile *f,
9fc0f6
 
9fc0f6
 found:
9fc0f6
         /* Let's cache this item for the next invocation */
9fc0f6
-        chain_cache_put(f->chain_cache, ci, first, a, o->entry_array.items[0], t);
9fc0f6
+        chain_cache_put(f->chain_cache, ci, first, a, o->entry_array.items[0], t, i);
9fc0f6
 
9fc0f6
         r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
9fc0f6
         if (r < 0)
9fc0f6
@@ -1463,11 +1467,12 @@ found:
9fc0f6
         return 1;
9fc0f6
 }
9fc0f6
 
9fc0f6
-static int generic_array_get_plus_one(JournalFile *f,
9fc0f6
-                                      uint64_t extra,
9fc0f6
-                                      uint64_t first,
9fc0f6
-                                      uint64_t i,
9fc0f6
-                                      Object **ret, uint64_t *offset) {
9fc0f6
+static int generic_array_get_plus_one(
9fc0f6
+                JournalFile *f,
9fc0f6
+                uint64_t extra,
9fc0f6
+                uint64_t first,
9fc0f6
+                uint64_t i,
9fc0f6
+                Object **ret, uint64_t *offset) {
9fc0f6
 
9fc0f6
         Object *o;
9fc0f6
 
9fc0f6
@@ -1498,17 +1503,18 @@ enum {
9fc0f6
         TEST_RIGHT
9fc0f6
 };
9fc0f6
 
9fc0f6
-static int generic_array_bisect(JournalFile *f,
9fc0f6
-                                uint64_t first,
9fc0f6
-                                uint64_t n,
9fc0f6
-                                uint64_t needle,
9fc0f6
-                                int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
9fc0f6
-                                direction_t direction,
9fc0f6
-                                Object **ret,
9fc0f6
-                                uint64_t *offset,
9fc0f6
-                                uint64_t *idx) {
9fc0f6
-
9fc0f6
-        uint64_t a, p, t = 0, i = 0, last_p = 0;
9fc0f6
+static int generic_array_bisect(
9fc0f6
+                JournalFile *f,
9fc0f6
+                uint64_t first,
9fc0f6
+                uint64_t n,
9fc0f6
+                uint64_t needle,
9fc0f6
+                int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
9fc0f6
+                direction_t direction,
9fc0f6
+                Object **ret,
9fc0f6
+                uint64_t *offset,
9fc0f6
+                uint64_t *idx) {
9fc0f6
+
9fc0f6
+        uint64_t a, p, t = 0, i = 0, last_p = 0, last_index = (uint64_t) -1;
9fc0f6
         bool subtract_one = false;
9fc0f6
         Object *o, *array = NULL;
9fc0f6
         int r;
9fc0f6
@@ -1533,7 +1539,7 @@ static int generic_array_bisect(JournalFile *f,
9fc0f6
                         return r;
9fc0f6
 
9fc0f6
                 if (r == TEST_LEFT) {
9fc0f6
-                        /* OK, what we are looking for is right of th
9fc0f6
+                        /* OK, what we are looking for is right of the
9fc0f6
                          * begin of this EntryArray, so let's jump
9fc0f6
                          * straight to previously cached array in the
9fc0f6
                          * chain */
9fc0f6
@@ -1541,6 +1547,7 @@ static int generic_array_bisect(JournalFile *f,
9fc0f6
                         a = ci->array;
9fc0f6
                         n -= ci->total;
9fc0f6
                         t = ci->total;
9fc0f6
+                        last_index = ci->last_index;
9fc0f6
                 }
9fc0f6
         }
9fc0f6
 
9fc0f6
@@ -1571,6 +1578,60 @@ static int generic_array_bisect(JournalFile *f,
9fc0f6
                 if (r == TEST_RIGHT) {
9fc0f6
                         left = 0;
9fc0f6
                         right -= 1;
9fc0f6
+
9fc0f6
+                        if (last_index != (uint64_t) -1) {
9fc0f6
+                                assert(last_index <= right);
9fc0f6
+
9fc0f6
+                                /* If we cached the last index we
9fc0f6
+                                 * looked at, let's try to not to jump
9fc0f6
+                                 * too wildly around and see if we can
9fc0f6
+                                 * limit the range to look at early to
9fc0f6
+                                 * the immediate neighbors of the last
9fc0f6
+                                 * index we looked at. */
9fc0f6
+
9fc0f6
+                                if (last_index > 0) {
9fc0f6
+                                        uint64_t x = last_index - 1;
9fc0f6
+
9fc0f6
+                                        p = le64toh(array->entry_array.items[x]);
9fc0f6
+                                        if (p <= 0)
9fc0f6
+                                                return -EBADMSG;
9fc0f6
+
9fc0f6
+                                        r = test_object(f, p, needle);
9fc0f6
+                                        if (r < 0)
9fc0f6
+                                                return r;
9fc0f6
+
9fc0f6
+                                        if (r == TEST_FOUND)
9fc0f6
+                                                r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
9fc0f6
+
9fc0f6
+                                        if (r == TEST_RIGHT)
9fc0f6
+                                                right = x;
9fc0f6
+                                        else
9fc0f6
+                                                left = x + 1;
9fc0f6
+                                }
9fc0f6
+
9fc0f6
+                                if (last_index < right) {
9fc0f6
+                                        uint64_t y = last_index + 1;
9fc0f6
+
9fc0f6
+                                        p = le64toh(array->entry_array.items[y]);
9fc0f6
+                                        if (p <= 0)
9fc0f6
+                                                return -EBADMSG;
9fc0f6
+
9fc0f6
+                                        r = test_object(f, p, needle);
9fc0f6
+                                        if (r < 0)
9fc0f6
+                                                return r;
9fc0f6
+
9fc0f6
+                                        if (r == TEST_FOUND)
9fc0f6
+                                                r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
9fc0f6
+
9fc0f6
+                                        if (r == TEST_RIGHT)
9fc0f6
+                                                right = y;
9fc0f6
+                                        else
9fc0f6
+                                                left = y + 1;
9fc0f6
+                                }
9fc0f6
+
9fc0f6
+                                last_index = (uint64_t) -1;
9fc0f6
+                        }
9fc0f6
+
9fc0f6
                         for (;;) {
9fc0f6
                                 if (left == right) {
9fc0f6
                                         if (direction == DIRECTION_UP)
9fc0f6
@@ -1581,8 +1642,8 @@ static int generic_array_bisect(JournalFile *f,
9fc0f6
                                 }
9fc0f6
 
9fc0f6
                                 assert(left < right);
9fc0f6
-
9fc0f6
                                 i = (left + right) / 2;
9fc0f6
+
9fc0f6
                                 p = le64toh(array->entry_array.items[i]);
9fc0f6
                                 if (p <= 0)
9fc0f6
                                         return -EBADMSG;
9fc0f6
@@ -1615,6 +1676,7 @@ static int generic_array_bisect(JournalFile *f,
9fc0f6
 
9fc0f6
                 n -= k;
9fc0f6
                 t += k;
9fc0f6
+                last_index = (uint64_t) -1;
9fc0f6
                 a = le64toh(array->entry_array.next_entry_array_offset);
9fc0f6
         }
9fc0f6
 
9fc0f6
@@ -1625,7 +1687,7 @@ found:
9fc0f6
                 return 0;
9fc0f6
 
9fc0f6
         /* Let's cache this item for the next invocation */
9fc0f6
-        chain_cache_put(f->chain_cache, ci, first, a, array->entry_array.items[0], t);
9fc0f6
+        chain_cache_put(f->chain_cache, ci, first, a, array->entry_array.items[0], t, i + (subtract_one ? -1 : 0));
9fc0f6
 
9fc0f6
         if (subtract_one && i == 0)
9fc0f6
                 p = last_p;
9fc0f6
@@ -1650,16 +1712,18 @@ found:
9fc0f6
         return 1;
9fc0f6
 }
9fc0f6
 
9fc0f6
-static int generic_array_bisect_plus_one(JournalFile *f,
9fc0f6
-                                         uint64_t extra,
9fc0f6
-                                         uint64_t first,
9fc0f6
-                                         uint64_t n,
9fc0f6
-                                         uint64_t needle,
9fc0f6
-                                         int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
9fc0f6
-                                         direction_t direction,
9fc0f6
-                                         Object **ret,
9fc0f6
-                                         uint64_t *offset,
9fc0f6
-                                         uint64_t *idx) {
9fc0f6
+
9fc0f6
+static int generic_array_bisect_plus_one(
9fc0f6
+                JournalFile *f,
9fc0f6
+                uint64_t extra,
9fc0f6
+                uint64_t first,
9fc0f6
+                uint64_t n,
9fc0f6
+                uint64_t needle,
9fc0f6
+                int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
9fc0f6
+                direction_t direction,
9fc0f6
+                Object **ret,
9fc0f6
+                uint64_t *offset,
9fc0f6
+                uint64_t *idx) {
9fc0f6
 
9fc0f6
         int r;
9fc0f6
         bool step_back = false;