diff mbox series

btrfs: remove pointless loop at btrfs_get_next_valid_item()

Message ID aa1b109d4ae1739890dfd379f10dc0540164e1cb.1680716909.git.fdmanana@suse.com (mailing list archive)
State New, archived
Headers show
Series btrfs: remove pointless loop at btrfs_get_next_valid_item() | expand

Commit Message

Filipe Manana April 5, 2023, 5:52 p.m. UTC
From: Filipe Manana <fdmanana@suse.com>

It's pointless to have a while loop at btrfs_get_next_valid_item(), as if
the slot on the current leaf is beyond the last item, we call
btrfs_next_leaf(), which leaves us at a valid slot of the next leaf (or
a valid slot in the current leaf if after releasing the path an item gets
pushed from the next leaf to the current leaf).

So just call btrfs_next_leaf() if the current slot on the current leaf is
beyond the last item.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/ctree.c | 23 ++++++-----------------
 1 file changed, 6 insertions(+), 17 deletions(-)

Comments

David Sterba April 6, 2023, 3:05 p.m. UTC | #1
On Wed, Apr 05, 2023 at 06:52:23PM +0100, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
> 
> It's pointless to have a while loop at btrfs_get_next_valid_item(), as if
> the slot on the current leaf is beyond the last item, we call
> btrfs_next_leaf(), which leaves us at a valid slot of the next leaf (or
> a valid slot in the current leaf if after releasing the path an item gets
> pushed from the next leaf to the current leaf).
> 
> So just call btrfs_next_leaf() if the current slot on the current leaf is
> beyond the last item.
> 
> Signed-off-by: Filipe Manana <fdmanana@suse.com>

Added to misc-next, thanks.
diff mbox series

Patch

diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index 3b956176b038..3c983c70028a 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -2490,26 +2490,15 @@  int btrfs_search_backwards(struct btrfs_root *root, struct btrfs_key *key,
 int btrfs_get_next_valid_item(struct btrfs_root *root, struct btrfs_key *key,
 			      struct btrfs_path *path)
 {
-	while (1) {
+	if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
 		int ret;
-		const int slot = path->slots[0];
-		const struct extent_buffer *leaf = path->nodes[0];
 
-		/* This is where we start walking the path. */
-		if (slot >= btrfs_header_nritems(leaf)) {
-			/*
-			 * If we've reached the last slot in this leaf we need
-			 * to go to the next leaf and reset the path.
-			 */
-			ret = btrfs_next_leaf(root, path);
-			if (ret)
-				return ret;
-			continue;
-		}
-		/* Store the found, valid item in @key. */
-		btrfs_item_key_to_cpu(leaf, key, slot);
-		break;
+		ret = btrfs_next_leaf(root, path);
+		if (ret)
+			return ret;
 	}
+
+	btrfs_item_key_to_cpu(path->nodes[0], key, path->slots[0]);
 	return 0;
 }