All of lore.kernel.org
 help / color / mirror / Atom feed
From: Matthew Wilcox <mawilcox@linuxonhyperv.com>
To: Yury Norov <yury.norov@gmail.com>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	George Spelvin <linux@horizon.com>,
	Akinobu Mita <akinobu.mita@gmail.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Andrew Morton <akpm@linux-foundation.org>,
	linux-kernel@vger.kernel.org
Cc: Matthew Wilcox <mawilcox@microsoft.com>
Subject: [PATCH] find_bit: Micro-optimise find_next_*_bit
Date: Fri, 23 Dec 2016 09:20:03 -0800	[thread overview]
Message-ID: <1482513603-9630-1-git-send-email-mawilcox@linuxonhyperv.com> (raw)

From: Matthew Wilcox <mawilcox@microsoft.com>

This saves 20 bytes on my x86-64 build, mostly due to alignment
considerations ... I think it actually saves about five bytes of
instructions.  There's really two parts to this commit.  First, the
first half of the test: (!nbits || start >= nbits) is trivially a subset
of the second half, since nbits and start are both unsigned.  Second,
while looking at the disassembly, I noticed that GCC was predicting the
branch taken.  Since this is a failure case, it's clearly the less likely
of the two branches, so add an unlikely() to override GCC's heuristics.

Signed-off-by: Matthew Wilcox <mawilcox@microsoft.com>
---
 lib/find_bit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/find_bit.c b/lib/find_bit.c
index 18072ea9c20e..7d4a681d625f 100644
--- a/lib/find_bit.c
+++ b/lib/find_bit.c
@@ -33,7 +33,7 @@ static unsigned long _find_next_bit(const unsigned long *addr,
 {
 	unsigned long tmp;
 
-	if (!nbits || start >= nbits)
+	if (unlikely(start >= nbits))
 		return nbits;
 
 	tmp = addr[start / BITS_PER_LONG] ^ invert;
-- 
2.11.0

             reply	other threads:[~2016-12-23 15:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-23 17:20 Matthew Wilcox [this message]
2016-12-23 20:43 ` [PATCH] find_bit: Micro-optimise find_next_*_bit Rasmus Villemoes
2016-12-24  6:43 ` Yury Norov

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=1482513603-9630-1-git-send-email-mawilcox@linuxonhyperv.com \
    --to=mawilcox@linuxonhyperv.com \
    --cc=akinobu.mita@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@horizon.com \
    --cc=linux@rasmusvillemoes.dk \
    --cc=mawilcox@microsoft.com \
    --cc=tglx@linutronix.de \
    --cc=yury.norov@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.