Re: RFC: turn kmalloc+memset(,0,) into kcalloc

Adrian Bunk
2005-04-07T21:49:40Z

On Tue, Apr 05, 2005 at 05:26:31PM +0100, Paulo Marques wrote:
>
> Hi,

Hi Paulo,

> I noticed there are a number of places in the kernel that do:
>
> ptr = kmalloc(n * size, ...)
> if (!ptr)
> goto out;
> memset(ptr, 0, n * size);
>
> It seems that these could be replaced by:
>
> ptr = kcalloc(n, size, ...)
> if (!ptr)
> goto out;
>
> saving a few bytes.
>...
> A quick (and lame) grep through the tree shows about 1200 of these
>cases. This means that about one quarter of all the kmallocs in the
>kernel are actually zeroed right after allocation.
>...
> pros:
> - smaller kernel image size
> - smaller (and more readable) source code
>...

Which is better readable depends on what you are used to.

> cons:
> - the NULL test is done twice
> - memset will not be optimized for constant sizes
>...
> Would this be a good thing to clean up, or isn't it worth the effort at all?
>...

You do plan to patch 1200 places in the kernel for this
micro-optimization?

This sounds like a really big overhead for a pretty small gain.

There are tasks of higher value that can be done.

E.g. read my "Stack usage tasks" email. The benefits would only be
present for people using GNU gcc 3.4 or SuSE gcc 3.3 on i386, but this
is a reasonable subset of the kernel users - and it brings them a
2% kernel size improvement.

cu
Adrian

--

"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon Seed