bcachefs: Kill btree_iter->journal_pos
authorKent Overstreet <kent.overstreet@linux.dev>
Fri, 17 Nov 2023 23:38:09 +0000 (18:38 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Mon, 1 Jan 2024 16:47:37 +0000 (11:47 -0500)
For BTREE_ITER_WITH_JOURNAL, we memoize lookups in the journal keys, to
avoid the binary search overhead.

Previously we stashed the pos of the last key returned from the journal,
in order to force the lookup to be redone when rewinding.

Now bch2_journal_keys_peek_upto() handles rewinding itself when
necessary - so we can slim down btree_iter.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/btree_iter.c
fs/bcachefs/btree_iter.h
fs/bcachefs/btree_journal_iter.c
fs/bcachefs/btree_types.h

index f58e03e..89592a4 100644 (file)
@@ -1854,19 +1854,11 @@ static struct bkey_i *bch2_btree_journal_peek(struct btree_trans *trans,
                                              struct btree_iter *iter,
                                              struct bpos end_pos)
 {
-       struct bkey_i *k;
-
-       if (bpos_lt(iter->path->pos, iter->journal_pos))
-               iter->journal_idx = 0;
-
-       k = bch2_journal_keys_peek_upto(trans->c, iter->btree_id,
-                                       iter->path->level,
-                                       iter->path->pos,
-                                       end_pos,
-                                       &iter->journal_idx);
-
-       iter->journal_pos = k ? k->k.p : end_pos;
-       return k;
+       return bch2_journal_keys_peek_upto(trans->c, iter->btree_id,
+                                          iter->path->level,
+                                          iter->path->pos,
+                                          end_pos,
+                                          &iter->journal_idx);
 }
 
 static noinline
index 0647201..9ef5021 100644 (file)
@@ -455,7 +455,6 @@ static inline void bch2_trans_iter_init_common(struct btree_trans *trans,
        iter->pos               = pos;
        iter->k                 = POS_KEY(pos);
        iter->journal_idx       = 0;
-       iter->journal_pos       = POS_MIN;
 #ifdef CONFIG_BCACHEFS_DEBUG
        iter->ip_allocated = ip;
 #endif
index ec52f50..7a5e0a8 100644 (file)
@@ -73,6 +73,7 @@ static size_t bch2_journal_key_search(struct journal_keys *keys,
        return idx_to_pos(keys, __bch2_journal_key_search(keys, id, level, pos));
 }
 
+/* Returns first non-overwritten key >= search key: */
 struct bkey_i *bch2_journal_keys_peek_upto(struct bch_fs *c, enum btree_id btree_id,
                                           unsigned level, struct bpos pos,
                                           struct bpos end_pos, size_t *idx)
@@ -86,12 +87,26 @@ search:
        if (!*idx)
                *idx = __bch2_journal_key_search(keys, btree_id, level, pos);
 
+       while (*idx &&
+              __journal_key_cmp(btree_id, level, end_pos, idx_to_key(keys, *idx - 1)) <= 0) {
+               --(*idx);
+               iters++;
+               if (iters == 10) {
+                       *idx = 0;
+                       goto search;
+               }
+       }
+
        while ((k = *idx < keys->nr ? idx_to_key(keys, *idx) : NULL)) {
                if (__journal_key_cmp(btree_id, level, end_pos, k) < 0)
                        return NULL;
 
-               if (__journal_key_cmp(btree_id, level, pos, k) <= 0 &&
-                   !k->overwritten)
+               if (k->overwritten) {
+                       (*idx)++;
+                       continue;
+               }
+
+               if (__journal_key_cmp(btree_id, level, pos, k) <= 0)
                        return k->k;
 
                (*idx)++;
index 662852b..ca75266 100644 (file)
@@ -304,7 +304,6 @@ struct btree_iter {
 
        /* BTREE_ITER_WITH_JOURNAL: */
        size_t                  journal_idx;
-       struct bpos             journal_pos;
 #ifdef TRACK_PATH_ALLOCATED
        unsigned long           ip_allocated;
 #endif