linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] include/linux/slab.h: use for() and left shift to calculate
@ 2021-03-02  3:39 Yejune Deng
  2021-03-02  3:52 ` Matthew Wilcox
  2021-03-02  9:09 ` Christoph Lameter
  0 siblings, 2 replies; 4+ messages in thread
From: Yejune Deng @ 2021-03-02  3:39 UTC (permalink / raw)
  To: cl, penberg, rientjes, iamjoonsoo.kim, akpm, vbabka
  Cc: linux-mm, linux-kernel, yejune.deng

use for() and left shift to calculate the value that compared with size.

Signed-off-by: Yejune Deng <yejune.deng@gmail.com>
---
 include/linux/slab.h | 32 ++++++++------------------------
 1 file changed, 8 insertions(+), 24 deletions(-)

diff --git a/include/linux/slab.h b/include/linux/slab.h
index 7ae604076767..0411f57930fb 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -347,6 +347,8 @@ static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
  */
 static __always_inline unsigned int kmalloc_index(size_t size)
 {
+	int i;
+
 	if (!size)
 		return 0;
 
@@ -357,30 +359,12 @@ static __always_inline unsigned int kmalloc_index(size_t size)
 		return 1;
 	if (KMALLOC_MIN_SIZE <= 64 && size > 128 && size <= 192)
 		return 2;
-	if (size <=          8) return 3;
-	if (size <=         16) return 4;
-	if (size <=         32) return 5;
-	if (size <=         64) return 6;
-	if (size <=        128) return 7;
-	if (size <=        256) return 8;
-	if (size <=        512) return 9;
-	if (size <=       1024) return 10;
-	if (size <=   2 * 1024) return 11;
-	if (size <=   4 * 1024) return 12;
-	if (size <=   8 * 1024) return 13;
-	if (size <=  16 * 1024) return 14;
-	if (size <=  32 * 1024) return 15;
-	if (size <=  64 * 1024) return 16;
-	if (size <= 128 * 1024) return 17;
-	if (size <= 256 * 1024) return 18;
-	if (size <= 512 * 1024) return 19;
-	if (size <= 1024 * 1024) return 20;
-	if (size <=  2 * 1024 * 1024) return 21;
-	if (size <=  4 * 1024 * 1024) return 22;
-	if (size <=  8 * 1024 * 1024) return 23;
-	if (size <=  16 * 1024 * 1024) return 24;
-	if (size <=  32 * 1024 * 1024) return 25;
-	if (size <=  64 * 1024 * 1024) return 26;
+
+	/* The maximum of size is 2^26 = 64 * 1024 * 1024 */
+	for (i = 3; i <= 26; i++) {
+		if (size <= (1 << i))
+			return i;
+	}
 	BUG();
 
 	/* Will never be reached. Needed because the compiler may complain */
-- 
2.29.0


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

* Re: [PATCH] include/linux/slab.h: use for() and left shift to calculate
  2021-03-02  3:39 [PATCH] include/linux/slab.h: use for() and left shift to calculate Yejune Deng
@ 2021-03-02  3:52 ` Matthew Wilcox
  2021-03-02  9:09 ` Christoph Lameter
  1 sibling, 0 replies; 4+ messages in thread
From: Matthew Wilcox @ 2021-03-02  3:52 UTC (permalink / raw)
  To: Yejune Deng
  Cc: cl, penberg, rientjes, iamjoonsoo.kim, akpm, vbabka, linux-mm,
	linux-kernel

On Tue, Mar 02, 2021 at 11:39:08AM +0800, Yejune Deng wrote:
> use for() and left shift to calculate the value that compared with size.

https://lore.kernel.org/linux-mm/339dbb54-b4bc-78e2-e3f0-986814e86d0e@suse.cz/

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

* Re: [PATCH] include/linux/slab.h: use for() and left shift to calculate
  2021-03-02  3:39 [PATCH] include/linux/slab.h: use for() and left shift to calculate Yejune Deng
  2021-03-02  3:52 ` Matthew Wilcox
@ 2021-03-02  9:09 ` Christoph Lameter
  2021-03-02 13:55   ` Yejune Deng
  1 sibling, 1 reply; 4+ messages in thread
From: Christoph Lameter @ 2021-03-02  9:09 UTC (permalink / raw)
  To: Yejune Deng
  Cc: penberg, rientjes, iamjoonsoo.kim, akpm, vbabka, linux-mm, linux-kernel

On Tue, 2 Mar 2021, Yejune Deng wrote:

> use for() and left shift to calculate the value that compared with size.

There is a reason for the madness...

The current code was written so compilers can do proper constant folding
and eliminate the whole function entirely.

If you want this change then please verify that all compilers currently in
use with this code do proper constant folding and never generate code for
the for loop.

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

* Re: [PATCH] include/linux/slab.h: use for() and left shift to calculate
  2021-03-02  9:09 ` Christoph Lameter
@ 2021-03-02 13:55   ` Yejune Deng
  0 siblings, 0 replies; 4+ messages in thread
From: Yejune Deng @ 2021-03-02 13:55 UTC (permalink / raw)
  To: Christoph Lameter, willy
  Cc: penberg, rientjes, iamjoonsoo.kim, akpm, vbabka, linux-mm,
	Linux Kernel Mailing List

Thank you. I hadn't thought of that。

On Tue, Mar 2, 2021 at 5:09 PM Christoph Lameter <cl@gentwo.de> wrote:
>
> On Tue, 2 Mar 2021, Yejune Deng wrote:
>
> > use for() and left shift to calculate the value that compared with size.
>
> There is a reason for the madness...
>
> The current code was written so compilers can do proper constant folding
> and eliminate the whole function entirely.
>
> If you want this change then please verify that all compilers currently in
> use with this code do proper constant folding and never generate code for
> the for loop.

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

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

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-02  3:39 [PATCH] include/linux/slab.h: use for() and left shift to calculate Yejune Deng
2021-03-02  3:52 ` Matthew Wilcox
2021-03-02  9:09 ` Christoph Lameter
2021-03-02 13:55   ` Yejune Deng

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