linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] buffer: a small optimization in grow_buffers
@ 2021-03-22 14:05 Mikulas Patocka
  2021-03-22 15:11 ` Matthew Wilcox
  2021-03-22 15:34 ` Al Viro
  0 siblings, 2 replies; 4+ messages in thread
From: Mikulas Patocka @ 2021-03-22 14:05 UTC (permalink / raw)
  To: Alexander Viro, Jens Axboe; +Cc: linux-fsdevel, linux-block

This patch replaces a loop with a "tzcnt" instruction.

Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>

Index: linux-2.6/fs/buffer.c
===================================================================
--- linux-2.6.orig/fs/buffer.c
+++ linux-2.6/fs/buffer.c
@@ -1020,11 +1020,7 @@ grow_buffers(struct block_device *bdev,
 	pgoff_t index;
 	int sizebits;
 
-	sizebits = -1;
-	do {
-		sizebits++;
-	} while ((size << sizebits) < PAGE_SIZE);
-
+	sizebits = PAGE_SHIFT - __ffs(size);
 	index = block >> sizebits;
 
 	/*


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

* Re: [PATCH] buffer: a small optimization in grow_buffers
  2021-03-22 14:05 [PATCH] buffer: a small optimization in grow_buffers Mikulas Patocka
@ 2021-03-22 15:11 ` Matthew Wilcox
  2021-03-22 16:36   ` Mikulas Patocka
  2021-03-22 15:34 ` Al Viro
  1 sibling, 1 reply; 4+ messages in thread
From: Matthew Wilcox @ 2021-03-22 15:11 UTC (permalink / raw)
  To: Mikulas Patocka; +Cc: Alexander Viro, Jens Axboe, linux-fsdevel, linux-block

On Mon, Mar 22, 2021 at 10:05:05AM -0400, Mikulas Patocka wrote:
> This patch replaces a loop with a "tzcnt" instruction.

Are you sure that's an optimisation?  The loop would execute very few
times under normal circumstances (a maximum of three times on x86).
Some numbers would be nice.

> Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
> 
> Index: linux-2.6/fs/buffer.c
> ===================================================================
> --- linux-2.6.orig/fs/buffer.c
> +++ linux-2.6/fs/buffer.c

Are ... are you still using CVS?!

> @@ -1020,11 +1020,7 @@ grow_buffers(struct block_device *bdev,
>  	pgoff_t index;
>  	int sizebits;
>  
> -	sizebits = -1;
> -	do {
> -		sizebits++;
> -	} while ((size << sizebits) < PAGE_SIZE);
> -
> +	sizebits = PAGE_SHIFT - __ffs(size);
>  	index = block >> sizebits;
>  
>  	/*
> 

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

* Re: [PATCH] buffer: a small optimization in grow_buffers
  2021-03-22 14:05 [PATCH] buffer: a small optimization in grow_buffers Mikulas Patocka
  2021-03-22 15:11 ` Matthew Wilcox
@ 2021-03-22 15:34 ` Al Viro
  1 sibling, 0 replies; 4+ messages in thread
From: Al Viro @ 2021-03-22 15:34 UTC (permalink / raw)
  To: Mikulas Patocka; +Cc: Jens Axboe, linux-fsdevel, linux-block

On Mon, Mar 22, 2021 at 10:05:05AM -0400, Mikulas Patocka wrote:
> This patch replaces a loop with a "tzcnt" instruction.
> 
> Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
> 
> Index: linux-2.6/fs/buffer.c
> ===================================================================
> --- linux-2.6.orig/fs/buffer.c
> +++ linux-2.6/fs/buffer.c
> @@ -1020,11 +1020,7 @@ grow_buffers(struct block_device *bdev,
>  	pgoff_t index;
>  	int sizebits;
>  
> -	sizebits = -1;
> -	do {
> -		sizebits++;
> -	} while ((size << sizebits) < PAGE_SIZE);
> -
> +	sizebits = PAGE_SHIFT - __ffs(size);
>  	index = block >> sizebits;
>  
>  	/*

Applied.

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

* Re: [PATCH] buffer: a small optimization in grow_buffers
  2021-03-22 15:11 ` Matthew Wilcox
@ 2021-03-22 16:36   ` Mikulas Patocka
  0 siblings, 0 replies; 4+ messages in thread
From: Mikulas Patocka @ 2021-03-22 16:36 UTC (permalink / raw)
  To: Matthew Wilcox; +Cc: Alexander Viro, Jens Axboe, linux-fsdevel, linux-block



On Mon, 22 Mar 2021, Matthew Wilcox wrote:

> On Mon, Mar 22, 2021 at 10:05:05AM -0400, Mikulas Patocka wrote:
> > This patch replaces a loop with a "tzcnt" instruction.
> 
> Are you sure that's an optimisation?  The loop would execute very few
> times under normal circumstances (a maximum of three times on x86).
> Some numbers would be nice.

According to Agner's instruction tables, tzcnt has latency 3 on Skylake 
and 2 on Zen. (386, 486, Pentium and K6 didn't have hardware for the bsf 
instruction, they executed it in microcode bit-by-bit, but newer 
processors execute it quickly).

The patch reduces code size by 16 bytes.

Mikulas

> > Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
> > 
> > Index: linux-2.6/fs/buffer.c
> > ===================================================================
> > --- linux-2.6.orig/fs/buffer.c
> > +++ linux-2.6/fs/buffer.c
> 
> Are ... are you still using CVS?!
> 
> > @@ -1020,11 +1020,7 @@ grow_buffers(struct block_device *bdev,
> >  	pgoff_t index;
> >  	int sizebits;
> >  
> > -	sizebits = -1;
> > -	do {
> > -		sizebits++;
> > -	} while ((size << sizebits) < PAGE_SIZE);
> > -
> > +	sizebits = PAGE_SHIFT - __ffs(size);
> >  	index = block >> sizebits;
> >  
> >  	/*
> > 
> 


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

end of thread, other threads:[~2021-03-22 16:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-22 14:05 [PATCH] buffer: a small optimization in grow_buffers Mikulas Patocka
2021-03-22 15:11 ` Matthew Wilcox
2021-03-22 16:36   ` Mikulas Patocka
2021-03-22 15:34 ` 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).