From: Sun YangKai <sunk67188@gmail.com>
To: sunk67188@gmail.com
Cc: linux-btrfs@vger.kernel.org
Subject: [PATCH v2 2/3] btrfs: early exit the searching process in search_tree ioctl
Date: Sat, 26 Jul 2025 21:51:40 +0800 [thread overview]
Message-ID: <20250726135214.16000-3-sunk67188@gmail.com> (raw)
In-Reply-To: <20250726135214.16000-1-sunk67188@gmail.com>
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 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
1.29user 21.02system 0:34.61elapsed 64%CPU without the patch, and
1.24user 17.34system 0:30.40elapsed 61%CPU with the patch.
When cache is hot, it's
1.21user 12.95system 0:14.19elapsed 99%CPU without the patch, and
1.18user 9.37system 0:10.57elapsed 99%CPU with the patch.
This is not a serious benchmark, but can reflect the performance
difference.
Signed-off-by: Sun YangKai <sunk67188@gmail.com>
---
fs/btrfs/ioctl.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 9948a12fd5e3..27c294e9d68d 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -1502,8 +1502,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 (!key_in_sk(key, sk)) {
+ ret = 1;
+ goto out;
+ }
if (sizeof(sh) + item_len > *buf_size) {
if (*num_found) {
--
2.50.0
next prev parent reply other threads:[~2025-07-26 13:52 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-12 4:31 [PATCH 0/3] btrfs: search_tree ioctl performance improvements and cleanups Sun YangKai
2025-06-12 4:31 ` [PATCH 1/3] btrfs: narrow loop variable scope in copy_to_sk() Sun YangKai
2025-06-12 4:31 ` [PATCH 2/3] btrfs: early exit the searching process in search_tree ioctl Sun YangKai
2025-06-12 4:31 ` [PATCH 3/3] btrfs: replace key_in_sk() with a simple btrfs_key compare Sun YangKai
2025-06-19 13:43 ` David Sterba
2025-06-19 14:31 ` Sun YangKai
2025-06-19 13:51 ` [PATCH 0/3] btrfs: search_tree ioctl performance improvements and cleanups David Sterba
2025-07-26 13:51 ` [PATCH v2 " Sun YangKai
2025-07-26 13:51 ` [PATCH v2 1/3] btrfs: narrow loop variable scope in copy_to_sk() Sun YangKai
2025-07-26 13:51 ` Sun YangKai [this message]
2025-07-26 13:51 ` [PATCH v2 3/3] btrfs: replace key_in_sk() with a simple btrfs_key compare Sun YangKai
2025-08-12 12:05 ` [PATCH v2 0/3] btrfs: search_tree ioctl performance improvements and cleanups Sun YangKai
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250726135214.16000-3-sunk67188@gmail.com \
--to=sunk67188@gmail.com \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).