mirror of https://lore.kernel.org/lkml/
 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 12/12] bitmap: don't count bits from the beginning in bitmap_remap()
Date: Mon, 28 Aug 2023 11:43:52 -0700	[thread overview]
Message-ID: <20230828184353.5145-13-yury.norov@gmail.com> (raw)
In-Reply-To: <20230828184353.5145-1-yury.norov@gmail.com>

In the remapping part, we count bits in 'old' and 'new' masks from the
beginning for every bit. Complexity of this approach is O(N^2). We can
do it better in O(N), if switch bitmap_remap() to using _from versions
of bitmap_weight() and find_nth_bit().

On kvm/x86_64 it works 9x faster than the current version for the 1000-bit
bitmap in lib/test_bitmap. Because popular distros like Ubuntu has 1024-bit
nodemasks, ~10x performance improvement is expected where bitmap_remap()
is used.

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

diff --git a/lib/bitmap.c b/lib/bitmap.c
index 1fca60d54cb4..8bca6bcf2bc5 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -996,7 +996,8 @@ 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 = 0, n;
+	unsigned int newbit, oldbit, w = 0, n;
+	unsigned int _oldbit, _newbit;
 
 	if (dst == src)		/* following doesn't handle inplace remaps */
 		return;
@@ -1005,8 +1006,17 @@ void __bitmap_remap(unsigned long *dst, const unsigned long *src,
 	bitmap_andnot(dst, src, old, nbits);
 
 	/* Remapping part */
+	_oldbit = _newbit = 0;
 	for_each_and_bit(oldbit, src, old, nbits) {
-		n = bitmap_weight(old, oldbit);
+		n = bitmap_weight_from(old, oldbit, _oldbit);
+		newbit = find_nth_bit_from(new, nbits, _newbit, n);
+		if (newbit < nbits)
+			goto set_bit;
+
+		/*
+		 * If we're out of bits in 'new' map, wrap
+		 * around and start from the beginning.
+		 */
 		if (w == 0) { /* if not initialized */
 			w = bitmap_weight(new, nbits);
 			if (w == 0) { /* if empty */
@@ -1014,7 +1024,13 @@ void __bitmap_remap(unsigned long *dst, const unsigned long *src,
 				return;
 			}
 		}
-		__set_bit(find_nth_bit(new, nbits, n % w), dst);
+
+		n -= bitmap_weight_from(new, nbits, _newbit);
+		newbit = find_nth_bit(new, nbits, n % w);
+set_bit:
+		__set_bit(newbit, dst);
+		_oldbit = oldbit;
+		_newbit = newbit;
 	}
 }
 EXPORT_SYMBOL(__bitmap_remap);
-- 
2.39.2


  parent reply	other threads:[~2023-08-28 18:45 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-28 18:43 [PATCH 00/12] bitmap: rework bitmap_{bit,}remap() Yury Norov
2023-08-28 18:43 ` [PATCH 01/12] bitmap: add find_nth_bit_from() Yury Norov
2023-08-28 18:43 ` [PATCH 02/12] bitmap: add bitmap_weight_from() Yury Norov
2023-08-29  7:22   ` Rasmus Villemoes
2023-08-28 18:43 ` [PATCH 03/12] bitmap: add test for bitmap_remap() Yury Norov
2023-08-28 18:43 ` [PATCH 04/12] bitmap: add test for bitmap_bitremap() Yury Norov
2023-08-28 18:43 ` [PATCH 05/12] bitmap: update comment for bitmap_{bit,}remap() Yury Norov
2023-08-28 18:43 ` [PATCH 06/12] bitmap: add small_cont_nbits() optimization for bitmap_remap() Yury Norov
2023-08-28 18:43 ` [PATCH 07/12] bitmap: add small_const_nbits() optimization for bitmap_bitremap() Yury Norov
2023-08-28 18:43 ` [PATCH 08/12] bitmap: optiimze bitmap_bitremap() Yury Norov
2023-08-28 18:43 ` [PATCH 09/12] bitmap: optimize bitmap_remap() when 'new' is empty map Yury Norov
2023-08-28 18:43 ` [PATCH 10/12] bitmap: separate handling of identity and remapping parts in bitmap_remap() Yury Norov
2023-08-28 18:43 ` [PATCH 11/12] bitmap: defer calculating weight of 'new' " Yury Norov
2023-08-28 18:43 ` Yury Norov [this message]
2023-08-29  7:33 ` [PATCH 00/12] bitmap: rework bitmap_{bit,}remap() Rasmus Villemoes
2023-08-29 13:38   ` Andy Shevchenko
2023-08-29 13:50     ` Yury Norov
2023-08-29 14: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=20230828184353.5145-13-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

all inboxes | Powered by JetHome®