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