<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wikidot="http://www.wikidot.com/rss-namespace">

	<channel>
		<title>Comments for page &quot;Using likely() and unlikely()&quot;</title>
		<link>http://250bpm.com/blog:6/comments/show</link>
		<description></description>
				<copyright></copyright>
		<lastBuildDate>Sat, 01 Aug 2015 21:43:08 +0000</lastBuildDate>
		
					<item>
				<guid>http://250bpm.com/blog:6/comments/show#post-1782905</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/blog:6/comments/show#post-1782905</link>
				<description></description>
				<pubDate>Sat, 25 May 2013 00:39:31 +0000</pubDate>
				<wikidot:authorName>bfig</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>In general you wouldn't want to accelerate a reactor leak more than you consider it to be likely&#8230;</p> <p>Just saying :).</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/blog:6/comments/show#post-1554957</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/blog:6/comments/show#post-1554957</link>
				<description></description>
				<pubDate>Mon, 03 Sep 2012 11:59:57 +0000</pubDate>
				<wikidot:authorName>marc</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>I think that to avoid cache misses you just can put the code right after the if condition. The compiler will usually keep the code in that order (first the if block, then the else block; it is still free to reorder single instructions) and so, the &quot;likely&quot; hint will have no effect whatsoever.</p> <p>I'm not an expert, I'm just thinking out loud. :)</p> <p>My point here is that I am not so sure about using the __builtin_expect hint for this kind of optimization. But, of course, I might be wrong! ;)</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/blog:6/comments/show#post-1554902</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/blog:6/comments/show#post-1554902</link>
				<description></description>
				<pubDate>Mon, 03 Sep 2012 10:30:12 +0000</pubDate>
				<wikidot:authorName>martin_sustrik</wikidot:authorName>				<wikidot:authorUserId>939</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>You are right that CPU cycles will be wasted. However, the point is that if you are not on a critical path you don't care. No time-sensitive computation is going on anyway.</p> <p>As for the prediction buffer, I guess there's nothing you can do about it, but even in such case the cache miss argument holds.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/blog:6/comments/show#post-1554889</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/blog:6/comments/show#post-1554889</link>
				<description></description>
				<pubDate>Mon, 03 Sep 2012 10:03:41 +0000</pubDate>
				<wikidot:authorName>marc</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>I'm not so sure.</p> <p>I think the point using __builtin_expect is branch prediction, as stated above, by jclairem.</p> <p>And the problem here is not only the cache misses the code can cause, but the wasted cycles in executing the branch marked as likely (or accelerated) when this is not the most probable branch to be executed. Even though you might want to optimize an unlikely branch because its execution time is critical, if you mark it as &quot;likely&quot; the CPU will start processing that branch every time (or most of the time) the function is called. So, the execution will then have to be thrown away and the CPU cycles will be wasted. Then the most probable executed branch (marked as &quot;unlikely&quot;, to optimize the other one) will have to be executed again and so, the pipeline slowed down because of the branch misprediction.</p> <p>Furthermore, the CPU has a branch prediction buffer (or something like that) so, if this function is called often, it will &quot;know&quot; that the most probable branch (marked as &quot;unlikely&quot;) is, in fact, the &quot;likely&quot; branch, making the CPU hint totally useless. In case it is not called so often, a branch misprediction will occur for every call.</p> <p>My apologies for the confusion created by the words [un]likely and most/less probable. It wasn't that easy to put. :P</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/blog:6/comments/show#post-1552728</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/blog:6/comments/show#post-1552728</link>
				<description></description>
				<pubDate>Fri, 31 Aug 2012 13:35:52 +0000</pubDate>
				<wikidot:authorName>jclairem</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>Sure I'm not saying otherwise and that is a good argument.</p> <p>I was just pointing out that the underlying performance issue is more related to branch mispredictions than cache misses.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/blog:6/comments/show#post-1552688</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/blog:6/comments/show#post-1552688</link>
				<description></description>
				<pubDate>Fri, 31 Aug 2012 12:37:19 +0000</pubDate>
				<wikidot:authorName>martin_sustrik</wikidot:authorName>				<wikidot:authorUserId>939</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>Yes, but does that break the argument above? The goal is not to tag the code as &quot;probable&quot; or &quot;unprobable&quot;, rather as &quot;I want this branch to be faster than the alternate branch&quot;.</p> <p>The most probable path may well not be on critical path and thus its performance is irrelevant. The least probable, on the other hand, can be directly on the critical path and should be optimised as much as possible.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/blog:6/comments/show#post-1552681</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/blog:6/comments/show#post-1552681</link>
				<description></description>
				<pubDate>Fri, 31 Aug 2012 12:23:24 +0000</pubDate>
				<wikidot:authorName>jclairem</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>You are missing (or maybe in purposely hiding) a fundamental issue that these macros try to solve: Branch Prediction. Modern CPUs are using pipelining. When CPU predicts the wrong branch it must flush its pipe. That's pretty bad in term of performance. With these macros compiler can add hints for the CPU.</p> <p>The whole cache thing is mostly hidden by instruction cache prefetching done by the CPU itself. Except if you write very sparse code&#8230;</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/blog:6/comments/show#post-1535661</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/blog:6/comments/show#post-1535661</link>
				<description></description>
				<pubDate>Mon, 13 Aug 2012 07:39:57 +0000</pubDate>
				<wikidot:authorName>martin_sustrik</wikidot:authorName>				<wikidot:authorUserId>939</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>Oops. You are right.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/blog:6/comments/show#post-1531273</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/blog:6/comments/show#post-1531273</link>
				<description></description>
				<pubDate>Wed, 08 Aug 2012 15:11:13 +0000</pubDate>
				<wikidot:authorName>Aszarsha</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>Your reasoning is wrong.</p> <p>if( unlikely( &#8230; ) ) { &#8230; } makes perfect sense.<br /> The if branch can be scheduled at the end of the function. Either it ret, call an unwind thunk, or jump back after the conditional check.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/blog:6/comments/show#post-1486898</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/blog:6/comments/show#post-1486898</link>
				<description></description>
				<pubDate>Fri, 22 Jun 2012 08:48:45 +0000</pubDate>
				<wikidot:authorName>martin_sustrik</wikidot:authorName>				<wikidot:authorUserId>939</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>The trouble is the if statement without an else clause. Then the likely/unlikely doesn't even make sense. Maybe a construct that enforces the it-then-else structure would help? fast_if (x) &#8230; else_slow &#8230; (the point would be to make compilation fail if else_slow clause would not be present)</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/blog:6/comments/show#post-1486892</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/blog:6/comments/show#post-1486892</link>
				<description></description>
				<pubDate>Fri, 22 Jun 2012 08:27:59 +0000</pubDate>
				<wikidot:authorName>Al Nonymous</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>&#8230;and now every condition is tagged with accelerate() in hopes it makes it execute faster. :)</p> <p>How about prioritize() ? Sounds appropriate. Not sure what the opposite should be though.</p> 
				 	]]>
				</content:encoded>							</item>
				</channel>
</rss>