Re: [RFC,PATCH] optimize fixed size kmalloc calls

Andrew Morton (akpm@digeo.com)
Tue, 10 Jun 2003 16:00:44 -0700


Brian Gerst <bgerst@didntduck.org> wrote:
>
> > What do you think about the attached patch? It's not pretty, but it
> > should work will all gcc versions. Any other ideas?
> > Just FYI: The function that forced me to use use switch/case instead of
> > if is interrupts_open in fs/proc/proc_misc.c.
>
> How about this? GCC 3.2.2 is able to optimize it away properly from the
> tests that I've run.

It doesn't compile with older gcc's - see the `extern' decl in the middle
of the function.

The compiler should have been given a way of disabling this. It'd going to
bite again and again.

I'll fix it up.

void *kmalloc(size_t size, int flags)
{
if (__builtin_constant_p(size)) {
int i = 0;

# 1 "include/linux/kmalloc_sizes.h" 1

if (size <= 32) goto found; else i++;

if (size <= 64) goto found; else i++;

if (size <= 128) goto found; else i++;

if (size <= 256) goto found; else i++;
if (size <= 512) goto found; else i++;
if (size <= 1024) goto found; else i++;
if (size <= 2048) goto found; else i++;
if (size <= 4096) goto found; else i++;
if (size <= 8192) goto found; else i++;
if (size <= 16384) goto found; else i++;
if (size <= 32768) goto found; else i++;
if (size <= 65536) goto found; else i++;
if (size <= 131072) goto found; else i++;
# 84 "include/linux/slab.h" 2

extern void __you_cannot_kmalloc_that_much(void);
__you_cannot_kmalloc_that_much();
found:
return kmem_cache_alloc((flags & 0x01) ?
malloc_sizes[i].cs_dmacachep :
malloc_sizes[i].cs_cachep, flags);
}
return __kmalloc(size, flags);
}

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/