From: Timothy Shimmin <tes@sgi.com>
To: Andi Kleen <andi@firstfloor.org>
Cc: David Chinner <dgc@sgi.com>, Niv Sardi <xaiki@cxhome.ath.cx>,
Christoph Hellwig <hch@lst.de>,
xfs@oss.sgi.com
Subject: likely and unlikely was: Re: [PATCH] split xfs_ioc_xattr
Date: Fri, 18 Apr 2008 17:06:50 +1000 [thread overview]
Message-ID: <4808488A.7010204@sgi.com> (raw)
In-Reply-To: <87ve2i5kbs.fsf@basil.nowhere.org>
Hi,
Thanks for the explanation, Andi.
So I guess the upshot is, that it can make a difference but
in many cases (where the perf difference isn't an issue)
it is probably not worth the ugliness.
And in performance cases, it would be best to test the hypothesis
with the unlikely profiler patch
=> it will be _unlikely_ we will bother ;-)
So I don't think I'll be bothering with them then unless
an issue comes up :)
--Tim
Andi Kleen wrote:
> Timothy Shimmin <tes@sgi.com> writes:
>> I'm still wondering if likely() and unlikely() should ever be used or not?
>
> It's more than just branch predictors. unlikely also moves unlikely
> code out of line and keeps it out of the icache (the obvious
> drawback is that it makes the asm code much harder to read
> during debugging though -- that is why it used to be turned off
> on x86-64)
>
> Then CPUs have two types of branch predictors: dynamic ones with
> history and static ones. Dynamic branch predictors tend to only work
> when the code has been recently executed several times and is still
> cached in their history buffers
>
> Now the nature of the kernel is that it is a library serving much more
> code running in user space. This leads to often the user space
> clearing out the history buffers and caches so kernel code has to often
> deal with running cache cold and also branch predictor cold.
>
> Then there are the static branch predictors in the CPU and unlikely()
> actually rearranges code to make them predict correctly.
>
> Personally I would say the cache effects (moving code out of line)
> are more important than the branch prediction because cache misses
> are more costly than branch misprediction.
>
> That all said it might make sense in some really performance critical code,
> especially if it's a in a loop and the gcc static branch predictors
> (gcc has a large range of builtin heuristics that say e.g. (x < 0) or
> (x == NULL) is unlikely). Most code is probably not performance critical
> enough to justify the ugliness of the code annotations. And again
> for many situations the builtin predictors of gcc (and the CPU) do
> fine without help anyways.
>
> Also if you add them you should at some point run with the unlikely
> profiler patch from mm just to make sure that your guesses about
> which paths are likely are actually correct. Humans are unfortunately
> often wrong on such guesses.
>
> Ideally (but that might ask for too much for normal code writing) you
> would only add them to code where you have oprofile data for branch
> mispredictions or icache misses.
>
> -Andi
>
next prev parent reply other threads:[~2008-04-18 7:07 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-03-19 20:40 [PATCH] split xfs_ioc_xattr Christoph Hellwig
2008-04-14 3:14 ` Niv Sardi
2008-04-14 3:29 ` Christoph Hellwig
2008-04-16 3:47 ` Niv Sardi
2008-04-16 6:37 ` David Chinner
2008-04-16 7:06 ` Timothy Shimmin
2008-04-16 7:29 ` Andi Kleen
2008-04-18 7:06 ` Timothy Shimmin [this message]
2008-04-18 13:34 ` likely and unlikely was: " Andi Kleen
2008-04-18 14:05 ` Eric Sandeen
2008-04-21 0:33 ` David Chinner
2008-04-21 7:55 ` Andi Kleen
2008-04-21 21:41 ` David Chinner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4808488A.7010204@sgi.com \
--to=tes@sgi.com \
--cc=andi@firstfloor.org \
--cc=dgc@sgi.com \
--cc=hch@lst.de \
--cc=xaiki@cxhome.ath.cx \
--cc=xfs@oss.sgi.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox