[PATCH 11/12] bitmap: defer calculating weight of 'new' in bitmap_remap()

From: Yury Norov
Date: Mon Aug 28 2023 - 14:45:18 EST


When 'old' map is empty, we don't need to calculate weight of 'new' map,
because all 'src' bits are simply copied to dst.

Signed-off-by: Yury Norov <yury.norov@xxxxxxxxx>
---
lib/bitmap.c | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/lib/bitmap.c b/lib/bitmap.c
index f62ea97e942c..1fca60d54cb4 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -996,23 +996,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, n;
+ unsigned int oldbit, w = 0, n;

if (dst == src) /* following doesn't handle inplace remaps */
return;

- w = bitmap_weight(new, nbits);
- if (w == 0) {
- bitmap_copy(dst, src, nbits);
- return;
- }
-
/* Identity part */
bitmap_andnot(dst, src, old, nbits);

/* Remapping part */
for_each_and_bit(oldbit, src, old, nbits) {
n = bitmap_weight(old, oldbit);
+ if (w == 0) { /* if not initialized */
+ w = bitmap_weight(new, nbits);
+ if (w == 0) { /* if empty */
+ bitmap_copy(dst, src, nbits);
+ return;
+ }
+ }
__set_bit(find_nth_bit(new, nbits, n % w), dst);
}
}
--
2.39.2