Performance tweaks

From: Andrew Morton (morton@nortelnetworks.com)
Date: Thu Feb 24 2000 - 07:53:59 EST


For uniprocessors this:

    const int cpu = smp_processor_id();
    if (dev->xmit_lock_owner != cpu) {
 
generates faster code than this:

    int cpu = smp_processor_id();
    if (dev->xmit_lock_owner != cpu) {

This is because the macro 'smp_processor_id()' evaluates to a constant.

When the identifier is declared const the compiler propagates this
knowledge and replaces it with the constant throughout. It's a win in
comparisons and in array subscripting.

Applying this to net/core/dev.c (4 places) saves ~70 bytes of text.
Ditto kernel/softirq.c. Not a huge space saving, but possibly a
measurable performance gain.

This applies to smp_num_cpus, smp_processor_id(),
hard_smp_processor_id(), smp_threads_ready, kernel_lock(),
cpu_logical_map(cpu), cpu_number_map(cpu) and probably lots of other
things.

There are other ways in which const declarations allow the compiler to
make additional optimisations. It works with pointers as well. 'char *
const foo = <expr>'. Stylistically nicer, too.

And while we're tweaking things:

arch/i386/mm/extable.c

static inline unsigned long
search_one_table(const struct exception_table_entry *first,
                 const struct exception_table_entry *last,
                 unsigned long value)
{
        while (first <= last) {
                const struct exception_table_entry *mid;
                long diff;

                mid = (last - first) / 2 + first;
                diff = mid->insn - value;
                if (diff == 0)
                        return mid->fixup;
                else if (diff < 0)
                        first = mid+1;
                else
                        last = mid-1;
        }
        return 0;
}

is sped up 10% for a 1000 entry table by moving the 'diff == 0'
comparison to the end. Least frequent last.

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



This archive was generated by hypermail 2b29 : Tue Feb 29 2000 - 21:00:09 EST