Linux-Fsdevel Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] fs/open.c: micro-optimization by avoiding branch on common path
@ 2020-09-19  0:10 mateusznosek0
  2020-09-19  0:20 ` Al Viro
  0 siblings, 1 reply; 2+ messages in thread
From: mateusznosek0 @ 2020-09-19  0:10 UTC (permalink / raw)
  To: linux-fsdevel, linux-kernel; +Cc: Mateusz Nosek, viro

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] fs/open.c: micro-optimization by avoiding branch on common path
  2020-09-19  0:10 [PATCH] fs/open.c: micro-optimization by avoiding branch on common path mateusznosek0
@ 2020-09-19  0:20 ` Al Viro
  0 siblings, 0 replies; 2+ messages in thread
From: Al Viro @ 2020-09-19  0:20 UTC (permalink / raw)
  To: mateusznosek0; +Cc: linux-fsdevel, linux-kernel

On Sat, Sep 19, 2020 at 02:10:21AM +0200, mateusznosek0@gmail.com wrote:
> 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.

Does it measurably show in any profiles?

> 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;
> +	}

If anything, turn the second if into return S_ISDIR(...) ? -EISDIR : -EINVAL;

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-09-19  0:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-19  0:10 [PATCH] fs/open.c: micro-optimization by avoiding branch on common path mateusznosek0
2020-09-19  0:20 ` Al Viro

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).