linux-bcachefs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Joshua Ashton <joshua@froggi.es>
To: linux-bcachefs@vger.kernel.org
Cc: Joshua Ashton <joshua@froggi.es>
Subject: [PATCH 2/4] bcachefs: Optimize bch2_dirent_name_bytes
Date: Sat, 12 Aug 2023 15:47:46 +0100	[thread overview]
Message-ID: <20230812145017.259609-2-joshua@froggi.es> (raw)
In-Reply-To: <20230812145017.259609-1-joshua@froggi.es>

Avoids doing a full strnlen for getting the length of the name of a
dirent entry.

Given the fact that the name of dirents is stored at the end of the
bkey's value, and we know the length of that in u64s, we can find the
last u64 and figure out how many NUL bytes are at the end of the string.

On little endian systems this ends up being the leading zeros of the
last u64, whereas on big endian systems this ends up being the trailing
zeros of the last u64.
We can take that value in bits and divide it by 8 to get the number of
NUL bytes at the end.

There is no endian-fixup or other compatibility here as this is string
data interpreted as a u64.

Signed-off-by: Joshua Ashton <joshua@froggi.es>
---
 fs/bcachefs/dirent.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/fs/bcachefs/dirent.c b/fs/bcachefs/dirent.c
index 065ea59ee9fa..b86c6c27424a 100644
--- a/fs/bcachefs/dirent.c
+++ b/fs/bcachefs/dirent.c
@@ -15,10 +15,18 @@
 
 unsigned bch2_dirent_name_bytes(struct bkey_s_c_dirent d)
 {
-	unsigned len = bkey_val_bytes(d.k) -
-		offsetof(struct bch_dirent, d_name);
-
-	return strnlen(d.v->d_name, len);
+	unsigned bkey_u64s = bkey_val_u64s(d.k);
+	unsigned bkey_bytes = bkey_u64s * sizeof(u64);
+	u64 last_u64 = ((u64*)d.v)[bkey_u64s - 1];
+#if CPU_BIG_ENDIAN
+	unsigned trailing_nuls = last_u64 ? __builtin_ctzll(last_u64) / 8 : 64 / 8;
+#else
+	unsigned trailing_nuls = last_u64 ? __builtin_clzll(last_u64) / 8 : 64 / 8;
+#endif
+
+	return bkey_bytes -
+		offsetof(struct bch_dirent, d_name) -
+		trailing_nuls;
 }
 
 static u64 bch2_dirent_hash(const struct bch_hash_info *info,
-- 
2.41.0


  reply	other threads:[~2023-08-12 14:50 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-12 14:47 [PATCH 1/4] bcachefs: Add btree_trans* to inode_set_fn Joshua Ashton
2023-08-12 14:47 ` Joshua Ashton [this message]
2023-08-12 16:23   ` [PATCH 2/4] bcachefs: Optimize bch2_dirent_name_bytes Kent Overstreet
2023-08-12 17:09     ` Joshua Ashton
2023-08-12 14:47 ` [PATCH 3/4] bcachefs: Introduce bch2_dirent_get_name Joshua Ashton
2023-08-12 14:47 ` [PATCH 4/4] bcachefs: Implement casefolding Joshua Ashton
2023-08-12 22:44   ` Kent Overstreet
2023-08-12 16:17 ` [PATCH 1/4] bcachefs: Add btree_trans* to inode_set_fn Kent Overstreet

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=20230812145017.259609-2-joshua@froggi.es \
    --to=joshua@froggi.es \
    --cc=linux-bcachefs@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).