public inbox for linux-kernel-mentees@lists.linux-foundation.org
 help / color / mirror / Atom feed
From: I Hsin Cheng <richard120310@gmail.com>
To: hirofumi@mail.parknet.co.jp
Cc: linux-kernel@vger.kernel.org, skhan@linuxfoundation.org,
	linux-kernel-mentees@lists.linux.dev, jserv@ccns.ncku.edu.tw,
	I Hsin Cheng <richard120310@gmail.com>
Subject: [PATCH] fat: Refactor fat_tolower with branchless implementation
Date: Tue, 18 Mar 2025 11:43:09 +0800	[thread overview]
Message-ID: <20250318034309.920866-1-richard120310@gmail.com> (raw)

Elimate the need of if-else branch within fat_tolower, replace it with a
branchless bitwise operation. This can reduce the number of branch ~130
regarding to the test script[1].

Test size can also be reduced:
$ ./scripts/bloat-o-meter vmlinux_old vmlinux_new
add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-68 (-68)
Function                                     old     new   delta
fat_parse_short                             1901    1833     -68
Total: Before=22471023, After=22470955, chg -0.00%

Signed-off-by: I Hsin Cheng <richard120310@gmail.com>
---
[1]:
static inline unsigned char old_tolower(unsigned char c)
{
    return ((c >= 'A') && (c <= 'Z')) ? c+32 : c;
}

static inline unsigned char new_tolower(unsigned char c)
{
    return c | 0x20;
}

int main(void) {
    for (unsigned char i = 0; i < 26; i++) {
        if (old_tolower('a' + i) != old_tolower('A' + i))
            return 1;
    }

    return 0;
}

Utilize perf to profile the difference when using old_tolower() and
new_tolower().

$ perf stat -e branches,branch-misses --repeat 100 ./old

 Performance counter stats for './old':

            2,6302      branches:u
              2334      branch-misses:u

       0.000754710 seconds time elapsed

       0.000000000 seconds user
       0.000804000 seconds sys

$ perf stat -e branches,branch-misses --repeat 100 ./new

 Performance counter stats for './main':

            2,6172      branches:u
              2338      branch-misses:u

       0.000782670 seconds time elapsed

       0.000853000 seconds user
       0.000000000 seconds sys

Best regards,
I Hsin Cheng
---
 fs/fat/dir.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/fat/dir.c b/fs/fat/dir.c
index acbec5bdd521..77d212b4d4db 100644
--- a/fs/fat/dir.c
+++ b/fs/fat/dir.c
@@ -35,7 +35,7 @@
 
 static inline unsigned char fat_tolower(unsigned char c)
 {
-	return ((c >= 'A') && (c <= 'Z')) ? c+32 : c;
+	return c | 0x20;
 }
 
 static inline loff_t fat_make_i_pos(struct super_block *sb,
-- 
2.43.0


             reply	other threads:[~2025-03-18  3:43 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-18  3:43 I Hsin Cheng [this message]
2025-03-18  3:53 ` [PATCH] fat: Refactor fat_tolower with branchless implementation OGAWA Hirofumi
2025-03-18  4:03 ` Al Viro
2025-03-19  2:49   ` I Hsin Cheng

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=20250318034309.920866-1-richard120310@gmail.com \
    --to=richard120310@gmail.com \
    --cc=hirofumi@mail.parknet.co.jp \
    --cc=jserv@ccns.ncku.edu.tw \
    --cc=linux-kernel-mentees@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=skhan@linuxfoundation.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