[PATCH] bitmap: optimize bitmap_remap()
From: Yury Norov
Date: Tue Aug 15 2023 - 20:00:20 EST
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@xxxxxxxxx>
---
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