Skip to contentSkip to conversation

btrfs: search_tree ioctl performance improvement

PATCH New Sun YangKai 1 participant 1 message Jan 14 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: search_tree ioctl performance improvement

With the following two assumptions(min_key and max_key here is the related fields in search_key):

1. When btrfs_search_forward returns 0, the key will always greater than or equal to the provided key. So the function copy_to_sk will never be called with a key less than the min_key. So we will never get a key less than min_key during walking forward in the leaf.

2. When we got a key greater than max_key in current leaf, we will never get a key in search_key range any more. So we should stop searching further.

So we can safely remove the comparing with min_key and replace the continue with early exiting. This will improve the performance of the search_tree related ioctl syscalls, which is the bottleneck of tools like compsize.

I've run compsize with and without this patch to compare the performance.

When cache is cold, it's
4.29user 28.48system 0:43.39elapsed 75%CPU without the patch, and 4.52user 22.53system 0:38.16elapsed 70%CPU with the patch.

When cache is hot, it's
4.10user 19.42system 0:23.52elapsed 99%CPU without the patch, and 4.23user 12.73system 0:16.96elapsed 99%CPU with the patch.

This is not a serious benchmark, but can reflect the performance difference.

---
 fs/btrfs/ioctl.c | 40 +++++++++-------------------------------
 1 file changed, 9 insertions(+), 31 deletions(-)
fs/btrfs/ioctl.c +9 -31
index 6eb6fe47074d..376c17c8233f 100644
@@ -1432,30 +1432,6 @@ static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
 	return ret;
 }
 
-static noinline bool key_in_sk(const struct btrfs_key *key,
-			       const struct btrfs_ioctl_search_key *sk)
-{
-	struct btrfs_key test;
-	int ret;
-
-	test.objectid = sk->min_objectid;
-	test.type = sk->min_type;
-	test.offset = sk->min_offset;
-
-	ret = btrfs_comp_cpu_keys(key, &test);
-	if (ret < 0)
-		return false;
-
-	test.objectid = sk->max_objectid;
-	test.type = sk->max_type;
-	test.offset = sk->max_offset;
-
-	ret = btrfs_comp_cpu_keys(key, &test);
-	if (ret > 0)
-		return false;
-	return true;
-}
-
 static noinline int copy_to_sk(struct btrfs_path *path,
 			       struct btrfs_key *key,
 			       const struct btrfs_ioctl_search_key *sk,
@@ -1467,7 +1443,7 @@ static noinline int copy_to_sk(struct btrfs_path *path,
 	u64 found_transid;
 	struct extent_buffer *leaf;
 	struct btrfs_ioctl_search_header sh;
-	struct btrfs_key test;
+	struct btrfs_key max_key;
 	unsigned long item_off;
 	unsigned long item_len;
 	int nritems;
@@ -1483,6 +1459,9 @@ static noinline int copy_to_sk(struct btrfs_path *path,
 		i = nritems;
 		goto advance_key;
 	}
+	max_key.objectid = sk->max_objectid;
+	max_key.type = sk->max_type;
+	max_key.offset = sk->max_offset;
 	found_transid = btrfs_header_generation(leaf);
 
 	for (i = slot; i < nritems; i++) {
@@ -1490,8 +1469,10 @@ static noinline int copy_to_sk(struct btrfs_path *path,
 		item_len = btrfs_item_size(leaf, i);
 
 		btrfs_item_key_to_cpu(leaf, key, i);
-		if (!key_in_sk(key, sk))
-			continue;
+		if (btrfs_comp_cpu_keys(key, &max_key) > 0) {
+			ret = 1;
+			goto out;
+		}
 
 		if (sizeof(sh) + item_len > *buf_size) {
 			if (*num_found) {
@@ -1560,10 +1541,7 @@ static noinline int copy_to_sk(struct btrfs_path *path,
 	}
 advance_key:
 	ret = 0;
-	test.objectid = sk->max_objectid;
-	test.type = sk->max_type;
-	test.offset = sk->max_offset;
-	if (btrfs_comp_cpu_keys(key, &test) >= 0)
+	if (btrfs_comp_cpu_keys(key, &max_key) >= 0)
 		ret = 1;
 	else if (key->offset < (u64)-1)
 		key->offset++;
Signature · 1 line
2.52.0