From mboxrd@z Thu Jan 1 00:00:00 1970 From: der.herr@hofr.at (Nicholas Mc Guire) Date: Wed, 18 Feb 2015 19:38:01 +0100 Subject: unlikely compiler flag propagation In-Reply-To: <20150218182453.GB21740@kroah.com> References: <20150218182453.GB21740@kroah.com> Message-ID: <20150218183801.GA1773@opentech.at> To: kernelnewbies@lists.kernelnewbies.org List-Id: kernelnewbies.lists.kernelnewbies.org On Wed, 18 Feb 2015, Greg KH wrote: > On Wed, Feb 18, 2015 at 07:09:47PM +0100, Matthias Brugger wrote: > > Hi all, > > > > I have a question about the unlikely compiler flag. > > When a called function is only returns an error with the unlikely flag > > set, should I set the unlikely compiler flag for the return value > > check in the callee as well? > > > > For example: > > > > int function_one(int *list, int num_elements) > > { > > int i; > > for (i =0; i < num_elements; i++) { > > if (unlikely(check_element(list + i))) > > return 1; > > } > > > > [...] > > > > return 0; > > } > > > > int function_two(...) > > { > > [...] > > > > if (function_one(list, num)) > > return -1; > > } > > > > > > So my question is, if function_two should instead implement: > > if (unlikely(function_one(list, num)) > > > > Or does the unlikely compiler flag propagate to calling functions? > > NEVER use unlikely/likely unless you can actually measure that it > matters if you use it. The compiler and processor is almost always > better at making these types of guesses and predictions, so let it do > the work instead. > > As proof of this, there was a test of the kernel a year or so ago that > measured the placement of the existing likely/unlikely markers in the > kernel and 90% of the usages were wrong and actually slowed down the > processor. > interesting - would you have a reference to some talk/paper/data/... ? > So just don't use it, unless you can measure it. > thx! hofrat