[PATCH 10/12] bitmap: separate handling of identity and remapping parts in bitmap_remap()

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


For unset bits in 'old' map (identity mapping), 'src' bits must be copied
to 'dst'. Doing that separately from remapping in a for-loop has some
advantages:
- implicitly initialize 'dst' without calling bitmap_zero();
- optimize performance of handling identity parts, because per-word
bitmap_andnot() is faster than per-bit set_bit() in a for-loop;
- make inner part of the loop unconditional;
- when 'old' map is empty, new logic simiply skips the for-loop part.

While here, replace set_bit() with a non-atomic version, because the whole
function is non-atomic anyways. If atomicity required, it should be handled
by user.

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

diff --git a/lib/bitmap.c b/lib/bitmap.c
index 50385d61e6ea..f62ea97e942c 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -996,11 +996,10 @@ 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 oldbit, w, n;

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

w = bitmap_weight(new, nbits);
if (w == 0) {
@@ -1008,13 +1007,13 @@ void __bitmap_remap(unsigned long *dst, const unsigned long *src,
return;
}

- for_each_set_bit(oldbit, src, nbits) {
- int n = bitmap_pos_to_ord(old, oldbit, nbits);
+ /* Identity part */
+ bitmap_andnot(dst, src, old, nbits);

- if (n < 0 || w == 0)
- set_bit(oldbit, dst); /* identity map */
- else
- set_bit(find_nth_bit(new, nbits, n % w), dst);
+ /* Remapping part */
+ for_each_and_bit(oldbit, src, old, nbits) {
+ n = bitmap_weight(old, oldbit);
+ __set_bit(find_nth_bit(new, nbits, n % w), dst);
}
}
EXPORT_SYMBOL(__bitmap_remap);
--
2.39.2