The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Yury Norov <yury.norov@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Yury Norov <yury.norov@gmail.com>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>
Subject: [PATCH] bitmap: optimize bitmap_remap()
Date: Tue, 15 Aug 2023 16:59:34 -0700	[thread overview]
Message-ID: <20230815235934.47782-1-yury.norov@gmail.com> (raw)

When 'new' map is empty, i.e. identity mapping, we can simply copy
src to dst, which is significantly faster than setting bits one by
one in a for-loop.

While here, replace set_bit() with non-atomic __set_bit().

Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 lib/bitmap.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/lib/bitmap.c b/lib/bitmap.c
index 24284caadbcc..bf6b0eea1af8 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -1004,20 +1004,24 @@ void bitmap_remap(unsigned long *dst, const unsigned long *src,
 		const unsigned long *old, const unsigned long *new,
 		unsigned int nbits)
 {
-	unsigned int oldbit, w;
+	unsigned int bit, oldbit, w;
 
 	if (dst == src)		/* following doesn't handle inplace remaps */
 		return;
-	bitmap_zero(dst, nbits);
 
 	w = bitmap_weight(new, nbits);
+	if (w == 0) {
+		bitmap_copy(dst, src, nbits);
+		return;
+	}
+
+	bitmap_zero(dst, nbits);
 	for_each_set_bit(oldbit, src, nbits) {
 		int n = bitmap_pos_to_ord(old, oldbit, nbits);
 
-		if (n < 0 || w == 0)
-			set_bit(oldbit, dst);	/* identity map */
-		else
-			set_bit(find_nth_bit(new, nbits, n % w), dst);
+		bit = (n < 0) ? oldbit :	/* identity map */
+				find_nth_bit(new, nbits, n % w);
+		__set_bit(bit, dst);
 	}
 }
 EXPORT_SYMBOL(bitmap_remap);
-- 
2.39.2


             reply	other threads:[~2023-08-16  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-15 23:59 Yury Norov [this message]
2023-08-17  9:37 ` [PATCH] bitmap: optimize bitmap_remap() Andy Shevchenko
2023-08-17  9:38   ` Andy Shevchenko
2023-08-17 14:21     ` Yury Norov
2023-08-17 15:37       ` Andy Shevchenko
2023-08-19  2:03         ` Yury Norov
2023-08-21  7:48           ` Rasmus Villemoes
2023-08-21  8:56             ` Andy Shevchenko

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=20230815235934.47782-1-yury.norov@gmail.com \
    --to=yury.norov@gmail.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    /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