All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH]micro optimization of kcalloc
@ 2006-03-20 13:08 Oliver Neukum
  0 siblings, 0 replies; 10+ messages in thread
From: Oliver Neukum @ 2006-03-20 13:08 UTC (permalink / raw)
  To: linux-kernel

Hi,

this transforms the limit check of kcalloc() so that size becomes the
divisor. This saves some kernel code, because size is always a constant,
thus turning the check into a simple comparison saving a full division.
This saved 18K in allyesconfig's kernel size.

	Regards
		Oliver

Signed-off-by: Oliver Neukum <oliver@neukum.name>

--- a/include/linux/slab.h	2006-03-11 23:12:55.000000000 +0100
+++ b/include/linux/slab.h	2006-03-20 09:00:41.000000000 +0100
@@ -118,7 +118,7 @@
  */
 static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
 {
-	if (n != 0 && size > INT_MAX / n)
+	if (unlikely(n > INT_MAX / size ))
 		return NULL;
 	return kzalloc(n * size, flags);
 }

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

* [PATCH]micro optimization of kcalloc
@ 2006-03-20 14:45 Oliver Neukum
  2006-03-20 15:14 ` Benjamin LaHaise
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Oliver Neukum @ 2006-03-20 14:45 UTC (permalink / raw)
  To: linux-kernel

Hi,

this optimises away a division in kcalloc by letting the compiler
do it. It is redone to allow size==0.

	Regards
		Oliver

Signed-off-by: Oliver Neukum <oliver@neukum.name>

--- linux-2.6.16-rc6-vanilla/include/linux/slab.h	2006-03-11 23:12:55.000000000 +0100
+++ linux-2.6.16-rc6/include/linux/slab.h	2006-03-20 14:39:36.000000000 +0100
@@ -118,7 +118,7 @@
  */
 static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
 {
-	if (n != 0 && size > INT_MAX / n)
+	if (unlikely(size != 0 && n > INT_MAX / size ))
 		return NULL;
 	return kzalloc(n * size, flags);
 }

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

* Re: [PATCH]micro optimization of kcalloc
  2006-03-20 14:45 [PATCH]micro optimization of kcalloc Oliver Neukum
@ 2006-03-20 15:14 ` Benjamin LaHaise
  2006-03-20 15:33   ` Oliver Neukum
  2006-03-20 16:15   ` Pekka Enberg
  2006-03-20 15:37 ` Pekka Enberg
  2006-03-20 15:51 ` Pekka Enberg
  2 siblings, 2 replies; 10+ messages in thread
From: Benjamin LaHaise @ 2006-03-20 15:14 UTC (permalink / raw)
  To: Oliver Neukum; +Cc: linux-kernel

On Mon, Mar 20, 2006 at 03:45:23PM +0100, Oliver Neukum wrote:
>  static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
>  {
> -	if (n != 0 && size > INT_MAX / n)
> +	if (unlikely(size != 0 && n > INT_MAX / size ))
>  		return NULL;
>  	return kzalloc(n * size, flags);
>  }

This function shouldn't be inlined.  We have no need to optimize the 
unlikely case like this.

		-ben
-- 
"Time is of no importance, Mr. President, only life is important."
Don't Email: <dont@kvack.org>.

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

* Re: [PATCH]micro optimization of kcalloc
  2006-03-20 15:33   ` Oliver Neukum
@ 2006-03-20 15:32     ` Benjamin LaHaise
  0 siblings, 0 replies; 10+ messages in thread
From: Benjamin LaHaise @ 2006-03-20 15:32 UTC (permalink / raw)
  To: Oliver Neukum; +Cc: linux-kernel

On Mon, Mar 20, 2006 at 04:33:29PM +0100, Oliver Neukum wrote:
> The likely case is passing constants. Without inlining precisely the
> likely case cannot be optimised.

That's what __builtin_constant_p() is for.

		-ben
-- 
"Time is of no importance, Mr. President, only life is important."
Don't Email: <dont@kvack.org>.

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

* Re: [PATCH]micro optimization of kcalloc
  2006-03-20 15:14 ` Benjamin LaHaise
@ 2006-03-20 15:33   ` Oliver Neukum
  2006-03-20 15:32     ` Benjamin LaHaise
  2006-03-20 16:15   ` Pekka Enberg
  1 sibling, 1 reply; 10+ messages in thread
From: Oliver Neukum @ 2006-03-20 15:33 UTC (permalink / raw)
  To: Benjamin LaHaise; +Cc: linux-kernel

Am Montag, 20. März 2006 16:14 schrieb Benjamin LaHaise:
> On Mon, Mar 20, 2006 at 03:45:23PM +0100, Oliver Neukum wrote:
> >  static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
> >  {
> > -	if (n != 0 && size > INT_MAX / n)
> > +	if (unlikely(size != 0 && n > INT_MAX / size ))
> >  		return NULL;
> >  	return kzalloc(n * size, flags);
> >  }
> 
> This function shouldn't be inlined.  We have no need to optimize the 
> unlikely case like this.

The likely case is passing constants. Without inlining precisely the
likely case cannot be optimised.

	Regards
		Oliver

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

* Re: [PATCH]micro optimization of kcalloc
  2006-03-20 14:45 [PATCH]micro optimization of kcalloc Oliver Neukum
  2006-03-20 15:14 ` Benjamin LaHaise
@ 2006-03-20 15:37 ` Pekka Enberg
  2006-03-20 15:51 ` Pekka Enberg
  2 siblings, 0 replies; 10+ messages in thread
From: Pekka Enberg @ 2006-03-20 15:37 UTC (permalink / raw)
  To: Oliver Neukum; +Cc: linux-kernel

On 3/20/06, Oliver Neukum <neukum@fachschaft.cup.uni-muenchen.de> wrote:
> --- linux-2.6.16-rc6-vanilla/include/linux/slab.h       2006-03-11 23:12:55.000000000 +0100
> +++ linux-2.6.16-rc6/include/linux/slab.h       2006-03-20 14:39:36.000000000 +0100
> @@ -118,7 +118,7 @@
>   */
>  static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
>  {
> -       if (n != 0 && size > INT_MAX / n)
> +       if (unlikely(size != 0 && n > INT_MAX / size ))

Please fix whitespace damage at the end.

                                     Pekka

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

* Re: [PATCH]micro optimization of kcalloc
  2006-03-20 14:45 [PATCH]micro optimization of kcalloc Oliver Neukum
  2006-03-20 15:14 ` Benjamin LaHaise
  2006-03-20 15:37 ` Pekka Enberg
@ 2006-03-20 15:51 ` Pekka Enberg
  2 siblings, 0 replies; 10+ messages in thread
From: Pekka Enberg @ 2006-03-20 15:51 UTC (permalink / raw)
  To: Oliver Neukum; +Cc: linux-kernel

On 3/20/06, Oliver Neukum <neukum@fachschaft.cup.uni-muenchen.de> wrote:
> this optimises away a division in kcalloc by letting the compiler
> do it. It is redone to allow size==0.

Does this shrink kernel text and if yes, how much?

                                        Pekka

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

* Re: [PATCH]micro optimization of kcalloc
  2006-03-20 15:14 ` Benjamin LaHaise
  2006-03-20 15:33   ` Oliver Neukum
@ 2006-03-20 16:15   ` Pekka Enberg
  2006-03-20 18:44     ` David Lang
  1 sibling, 1 reply; 10+ messages in thread
From: Pekka Enberg @ 2006-03-20 16:15 UTC (permalink / raw)
  To: Benjamin LaHaise; +Cc: Oliver Neukum, linux-kernel, Adrian Bunk

Hi,

On Mon, Mar 20, 2006 at 03:45:23PM +0100, Oliver Neukum wrote:
> >  static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
> >  {
> > -     if (n != 0 && size > INT_MAX / n)
> > +     if (unlikely(size != 0 && n > INT_MAX / size ))
> >               return NULL;
> >       return kzalloc(n * size, flags);
> >  }

On 3/20/06, Benjamin LaHaise <bcrl@kvack.org> wrote:
> This function shouldn't be inlined.  We have no need to optimize the
> unlikely case like this.

IIRC, I made it static inline in the first place because that actually
reduced kernel text size. (And I think it was Adrian who made me do it
:-).

                                Pekka

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

* Re: [PATCH]micro optimization of kcalloc
  2006-03-20 16:15   ` Pekka Enberg
@ 2006-03-20 18:44     ` David Lang
  2006-03-22  5:40       ` Valdis.Kletnieks
  0 siblings, 1 reply; 10+ messages in thread
From: David Lang @ 2006-03-20 18:44 UTC (permalink / raw)
  To: Pekka Enberg; +Cc: Benjamin LaHaise, Oliver Neukum, linux-kernel, Adrian Bunk

On Mon, 20 Mar 2006, Pekka Enberg wrote:

> Hi,
>
> On Mon, Mar 20, 2006 at 03:45:23PM +0100, Oliver Neukum wrote:
>>>  static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
>>>  {
>>> -     if (n != 0 && size > INT_MAX / n)
>>> +     if (unlikely(size != 0 && n > INT_MAX / size ))
>>>               return NULL;
>>>       return kzalloc(n * size, flags);
>>>  }
>
> On 3/20/06, Benjamin LaHaise <bcrl@kvack.org> wrote:
>> This function shouldn't be inlined.  We have no need to optimize the
>> unlikely case like this.
>
> IIRC, I made it static inline in the first place because that actually
> reduced kernel text size. (And I think it was Adrian who made me do it
> :-).

I wonder if this is still needed with the new inline changes that were 
made to allow GCC to make the decision (for recent GCC's)

David Lang

-- 
There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies.
  -- C.A.R. Hoare


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

* Re: [PATCH]micro optimization of kcalloc
  2006-03-20 18:44     ` David Lang
@ 2006-03-22  5:40       ` Valdis.Kletnieks
  0 siblings, 0 replies; 10+ messages in thread
From: Valdis.Kletnieks @ 2006-03-22  5:40 UTC (permalink / raw)
  To: David Lang
  Cc: Pekka Enberg, Benjamin LaHaise, Oliver Neukum, linux-kernel,
	Adrian Bunk

[-- Attachment #1: Type: text/plain, Size: 1052 bytes --]

On Mon, 20 Mar 2006 10:44:00 PST, David Lang said:
> On Mon, 20 Mar 2006, Pekka Enberg wrote:

> > On Mon, Mar 20, 2006 at 03:45:23PM +0100, Oliver Neukum wrote:
> >>>  static inline void *kcalloc(size_t n, size_t size, gfp_t flags)

> > On 3/20/06, Benjamin LaHaise <bcrl@kvack.org> wrote:
> >> This function shouldn't be inlined.  We have no need to optimize the
> >> unlikely case like this.
> >
> > IIRC, I made it static inline in the first place because that actually
> > reduced kernel text size. (And I think it was Adrian who made me do it
> > :-).
> 
> I wonder if this is still needed with the new inline changes that were 
> made to allow GCC to make the decision (for recent GCC's)

One non-obvious reason to inline it (at least in -mm kernels) is because the
slab leak detector stuff wants to find where it was called from - and if you
don't inline kcalloc(), you end up with the kzalloc() call it makes showing
kcalloc() as the caller.  If you inline it, you end up showing the caller
of kcalloc() instead, which is far more useful.....

[-- Attachment #2: Type: application/pgp-signature, Size: 228 bytes --]

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

end of thread, other threads:[~2006-03-22  5:44 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-03-20 14:45 [PATCH]micro optimization of kcalloc Oliver Neukum
2006-03-20 15:14 ` Benjamin LaHaise
2006-03-20 15:33   ` Oliver Neukum
2006-03-20 15:32     ` Benjamin LaHaise
2006-03-20 16:15   ` Pekka Enberg
2006-03-20 18:44     ` David Lang
2006-03-22  5:40       ` Valdis.Kletnieks
2006-03-20 15:37 ` Pekka Enberg
2006-03-20 15:51 ` Pekka Enberg
  -- strict thread matches above, loose matches on Subject: below --
2006-03-20 13:08 Oliver Neukum

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.