diff mbox series

[2/3] btrfs: remove unnecessary 'found_key' local variable in btrfs_search_forward()

Message ID 20250309075820.30999-3-sunk67188@gmail.com (mailing list archive)
State New
Headers show
Series btrfs: random code cleanup | expand

Commit Message

Sun YangKai March 9, 2025, 7:58 a.m. UTC
The 'found_key' variable was only used to temporarily store a key value
before copying it to 'min_key' at the end of the function when returning
success (ret=0).

This commit optimizes the code by:
1. Eliminating the intermediate 'found_key' variable
2. Directly populating 'min_key' at the exact loop exit points where
   ret=0 is set
3. Removing the final memcpy operation in the return path

This change improves code clarity by:
- Removing redundant variable usage
- Simplifying the success path logic
- Reducing memory operations

The found key value is now stored directly into the destination 'min_key'
structure at the point of discovery, maintaining identical functionality
while:
- Eliminating an unnecessary memory copy
- Reducing code complexity

Signed-off-by: Sun YangKai <sunk67188@gmail.com>
---
 fs/btrfs/ctree.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index 3dc5a35dd19b..1dc59dc3b708 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -4608,7 +4608,6 @@  int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
 			 u64 min_trans)
 {
 	struct extent_buffer *cur;
-	struct btrfs_key found_key;
 	int slot;
 	int sret;
 	u32 nritems;
@@ -4644,7 +4643,8 @@  int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
 				goto find_next_key;
 			ret = 0;
 			path->slots[level] = slot;
-			btrfs_item_key_to_cpu(cur, &found_key, slot);
+			/* save our key for returning back */
+			btrfs_item_key_to_cpu(cur, min_key, slot);
 			goto out;
 		}
 		if (sret && slot > 0)
@@ -4679,11 +4679,11 @@  int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
 				goto out;
 			}
 		}
-		/* save our key for returning back */
-		btrfs_node_key_to_cpu(cur, &found_key, slot);
 		path->slots[level] = slot;
 		if (level == path->lowest_level) {
 			ret = 0;
+			/* save our key for returning back */
+			btrfs_node_key_to_cpu(cur, min_key, slot);
 			goto out;
 		}
 		cur = btrfs_read_node_slot(cur, slot);
@@ -4702,7 +4702,6 @@  int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
 	path->keep_locks = keep_locks;
 	if (ret == 0) {
 		btrfs_unlock_up_safe(path, path->lowest_level + 1);
-		memcpy(min_key, &found_key, sizeof(found_key));
 	}
 	return ret;
 }