From: mateusznosek0@gmail.com
To: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Mateusz Nosek <mateusznosek0@gmail.com>, viro@zeniv.linux.org.uk
Subject: [PATCH] fs/open.c: micro-optimization by avoiding branch on common path
Date: Sat, 19 Sep 2020 02:10:21 +0200 [thread overview]
Message-ID: <20200919001021.21690-1-mateusznosek0@gmail.com> (raw)
From: Mateusz Nosek <mateusznosek0@gmail.com>
If file is a directory it is surely not regular. Therefore, if 'S_ISREG'
check returns false one can be sure that vfs_truncate must returns with
error. Introduced patch refactors code to avoid one branch in 'likely'
control flow path. Moreover, it marks the proper check with 'unlikely'
macro to improve both branch prediction and readability. Changes were
tested with gcc 8.3.0 on x86 architecture and it is confirmed that
slightly better assembly is generated.
Signed-off-by: Mateusz Nosek <mateusznosek0@gmail.com>
---
fs/open.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/fs/open.c b/fs/open.c
index 9af548fb841b..69658ea27530 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -74,10 +74,12 @@ long vfs_truncate(const struct path *path, loff_t length)
inode = path->dentry->d_inode;
/* For directories it's -EISDIR, for other non-regulars - -EINVAL */
- if (S_ISDIR(inode->i_mode))
- return -EISDIR;
- if (!S_ISREG(inode->i_mode))
- return -EINVAL;
+ if (unlikely(!S_ISREG(inode->i_mode))) {
+ if (S_ISDIR(inode->i_mode))
+ return -EISDIR;
+ else
+ return -EINVAL;
+ }
error = mnt_want_write(path->mnt);
if (error)
--
2.20.1
next reply other threads:[~2020-09-19 0:10 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-19 0:10 mateusznosek0 [this message]
2020-09-19 0:20 ` Al Viro
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=20200919001021.21690-1-mateusznosek0@gmail.com \
--to=mateusznosek0@gmail.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
/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
Powered by JetHome