bcachefs: Kill bpos_diff() XXX check for perf regression
authorKent Overstreet <kent.overstreet@gmail.com>
Mon, 30 Aug 2021 19:18:31 +0000 (15:18 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:09:11 +0000 (17:09 -0400)
This improves the btree iterator lookup code by using
trans_for_each_iter_inorder().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
fs/bcachefs/bkey.h
fs/bcachefs/btree_iter.c

index 72b4267031d8d67906a42e2e2f21c80b0b7b9e0a..904ceb67a0291d4f011ebc40384290364ab55eed 100644 (file)
@@ -171,37 +171,6 @@ static inline struct bpos bpos_max(struct bpos l, struct bpos r)
        return bpos_cmp(l, r) > 0 ? l : r;
 }
 
-#define sbb(a, b, borrow)                              \
-do {                                                   \
-       typeof(a) d1, d2;                               \
-                                                       \
-       d1 = a - borrow;                                \
-       borrow  = d1 > a;                               \
-                                                       \
-       d2 = d1 - b;                                    \
-       borrow += d2 > d1;                              \
-       a = d2;                                         \
-} while (0)
-
-/* returns a - b: */
-static inline struct bpos bpos_sub(struct bpos a, struct bpos b)
-{
-       int borrow = 0;
-
-       sbb(a.snapshot, b.snapshot,     borrow);
-       sbb(a.offset,   b.offset,       borrow);
-       sbb(a.inode,    b.inode,        borrow);
-       return a;
-}
-
-static inline struct bpos bpos_diff(struct bpos l, struct bpos r)
-{
-       if (bpos_cmp(l, r) > 0)
-               swap(l, r);
-
-       return bpos_sub(r, l);
-}
-
 void bch2_bpos_swab(struct bpos *);
 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
 
index 06379f3e40a648a8d1bd0c308cadbc65a3fb324b..a856f5e90727360d1f756fb3a54c32a43c1ae19d 100644 (file)
@@ -1739,36 +1739,35 @@ struct btree_path *bch2_path_get(struct btree_trans *trans, bool cached,
                                 unsigned locks_want, unsigned level,
                                 bool intent)
 {
-       struct btree_path *path, *best = NULL;
+       struct btree_path *path, *path_pos = NULL;
        struct bpos pos_min = POS_MIN;
        int i;
 
        BUG_ON(trans->restarted);
 
-       trans_for_each_path(trans, path) {
-               if (path->cached != cached ||
-                   path->btree_id != btree_id ||
-                   path->level != level)
-                       continue;
-
-               if (best) {
-                       int cmp = bkey_cmp(bpos_diff(best->pos, pos),
-                                          bpos_diff(path->pos, pos));
+       btree_trans_sort_paths(trans);
 
-                       if (cmp < 0 ||
-                           ((cmp == 0 && (path->ref || path->preserve))))
-                               continue;
-               }
+       trans_for_each_path_inorder(trans, path, i) {
+               if (__btree_path_cmp(path,
+                                    btree_id,
+                                    cached,
+                                    pos,
+                                    level) > 0)
+                       break;
 
-               best = path;
+               path_pos = path;
        }
 
-       if (best) {
-               __btree_path_get(best, intent);
-               path = btree_path_set_pos(trans, best, pos, intent);
+       if (path_pos &&
+           path_pos->cached    == cached &&
+           path_pos->btree_id  == btree_id &&
+           path_pos->level     == level) {
+               __btree_path_get(path_pos, intent);
+               path = btree_path_set_pos(trans, path_pos, pos, intent);
                path->preserve = true;
        } else {
-               path = btree_path_alloc(trans, NULL);
+               path = btree_path_alloc(trans, path_pos);
+               path_pos = NULL;
 
                __btree_path_get(path, intent);
                path->pos                       = pos;
@@ -1808,9 +1807,9 @@ struct btree_path *bch2_path_get(struct btree_trans *trans, bool cached,
 
        trace_trans_get_path(_RET_IP_, trans->ip, btree_id,
                             &pos, locks_want, path->uptodate,
-                            best ? &best->pos          : &pos_min,
-                            best ? best->locks_want    : U8_MAX,
-                            best ? best->uptodate      : U8_MAX);
+                            path_pos ? &path_pos->pos          : &pos_min,
+                            path_pos ? path_pos->locks_want    : U8_MAX,
+                            path_pos ? path_pos->uptodate      : U8_MAX);
 
        return path;
 }