Re: When should we use likely() / unlikely() / get_unaligned() ?

From: Rusty Russell
Date: Sun Feb 08 2004 - 05:58:29 EST


On Fri, 06 Feb 2004 11:06:19 +0000
David Woodhouse <dwmw2@xxxxxxxxxxxxx> wrote:

> There seems to be no coherent answer to the above questions. On some
> architectures likely() might bypass dynamic branch prediction, so we
> shouldn't use it unless there's at _least_ a 95% probability; on others
> it may simply affect code ordering and we gain a tiny benefit from it if
> the probabilities aren't precisely 50/50.

Yes, agreed. But many unlikely() macros are simply there because gcc isn't
smart enough yet:

eg. fs/read_write.c:

if (unlikely(put_user(pos, offset)))
return -EFAULT;

It'd be better to have gcc know that this function was unlikely to return
a -ve value, and derive all the error paths itself.

It'd also be nice to be able to mark eg. printk() and BUG() as fundamentally
unlikely.

Sometimes, unlikely()/likely() help code readability. But generally it
should be considered the register keyword of the 2000's: if the case isn't
ABSOLUTELY CRYSTAL CLEAR, or doesn't show up on benchmarks, distain is
appropriate.

Cheers,
Rusty.
--
there are those who do and those who hang on and you don't see too
many doers quoting their contemporaries. -- Larry McVoy
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/