diff mbox series

buffer: a small optimization in grow_buffers

Message ID alpine.LRH.2.02.2103221002360.19948@file01.intranet.prod.int.rdu2.redhat.com (mailing list archive)
State New, archived
Headers show
Series buffer: a small optimization in grow_buffers | expand

Commit Message

Mikulas Patocka March 22, 2021, 2:05 p.m. UTC
This patch replaces a loop with a "tzcnt" instruction.

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

Comments

Matthew Wilcox March 22, 2021, 3:11 p.m. UTC | #1
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;
>  
>  	/*
>
Al Viro March 22, 2021, 3:34 p.m. UTC | #2
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.
Mikulas Patocka March 22, 2021, 4:36 p.m. UTC | #3
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;
> >  
> >  	/*
> > 
>
diff mbox series

Patch

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;
 
 	/*