Skip to contentSkip to conversation

wifi: carl9170: micro-optimize carl9170_tx_shift_bm()

PATCH Applied Yury Norov 3 participants 6 messages Mar 26, 2025 – May 21, 2025 CARL9170 Linux community wireless driver

lore
Share & tools
lore

Yury Norov

author
#0 lore raw

PATCH 1/1 wifi: carl9170: micro-optimize carl9170_tx_shift_bm()

The function calls bitmap_empty() just before find_first_bit(). Both functions are O(N). Because find_first_bit() returns >= nbits in case of empty bitmap, the bitmap_empty() test may be avoided.

---
 drivers/net/wireless/ath/carl9170/tx.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/wireless/ath/carl9170/tx.c +1 -2
index 0226c31a6cae..b7717f9e1e9b 100644
@@ -366,8 +366,7 @@ static void carl9170_tx_shift_bm(struct ar9170 *ar,
 	if (WARN_ON_ONCE(off >= CARL9170_BAW_BITS))
 		return;
 
-	if (!bitmap_empty(tid_info->bitmap, off))
-		off = find_first_bit(tid_info->bitmap, off);
+	off = min(off, find_first_bit(tid_info->bitmap, off));
 
 	tid_info->bsn += off;
 	tid_info->bsn &= 0x0fff;
Signature · 1 line
2.43.0
Acked-by on v1 1/1
#1 lore raw

Hi,

Quoted 5 lines · Yury Norov

The function calls bitmap_empty() just before find_first_bit(). Both functions are O(N). Because find_first_bit() returns >= nbits in case of empty bitmap, the bitmap_empty() test may be avoided.

I looked up bitmap_empty():
<https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/bitmap.h#n423>

apart from the small_const_nbits stuff (which carl9170 likely does not qualify for since from what I remember it's a 128bits bitmap) the function just does:

| return find_first_bit(src, nbits) == nbits;

so yes, find_first_bit runs twice with same parameters... Unless the compiler is smart
enough to detect this and (re-)use the intermediate result later. But I haven't check
if this is the case with any current, old or future compilers. Has anyone?

Anyway, Sure.

Quoted 1 line
Quoted 21 lines
---
 drivers/net/wireless/ath/carl9170/tx.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/wireless/ath/carl9170/tx.c +1 -3
index 0226c31a6cae..b7717f9e1e9b 100644
@@ -366,8 +366,7 @@ static void carl9170_tx_shift_bm(struct ar9170 *ar,
        if (WARN_ON_ONCE(off >= CARL9170_BAW_BITS))
                return;
 
-       if (!bitmap_empty(tid_info->bitmap, off))
-               off = find_first_bit(tid_info->bitmap, off);
+       off = min(off, find_first_bit(tid_info->bitmap, off));
 
        tid_info->bsn += off;
        tid_info->bsn &= 0x0fff;
--

2.43.0

author
#2 lore raw
Quoted 23 lines · Christian Lamparter

Hi,

Quoted 5 lines · Yury Norov

The function calls bitmap_empty() just before find_first_bit(). Both functions are O(N). Because find_first_bit() returns >= nbits in case of empty bitmap, the bitmap_empty() test may be avoided.

I looked up bitmap_empty():
<https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/bitmap.h#n423>

apart from the small_const_nbits stuff (which carl9170 likely does not qualify for since from what I remember it's a 128bits bitmap) the function just does:

| return find_first_bit(src, nbits) == nbits;

so yes, find_first_bit runs twice with same parameters... Unless the compiler is smart
enough to detect this and (re-)use the intermediate result later. But I haven't check
if this is the case with any current, old or future compilers. Has anyone?

Quoted 5 lines

Anyway, Sure.

Quoted 1 line

Thanks, Chrustian. So, how is that supposed to be merged? I can move it with bitmap-for-next, unless there's no better branch.

Thanks,
Yury

#3 lore raw
Quoted 28 lines · Yury Norov
Quoted 27 lines · Christian Lamparter

Hi,

Quoted 5 lines · Yury Norov

The function calls bitmap_empty() just before find_first_bit(). Both functions are O(N). Because find_first_bit() returns >= nbits in case of empty bitmap, the bitmap_empty() test may be avoided.

I looked up bitmap_empty():
<https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/bitmap.h#n423>

apart from the small_const_nbits stuff (which carl9170 likely does not qualify for since from what I remember it's a 128bits bitmap) the function just does:

| return find_first_bit(src, nbits) == nbits;

so yes, find_first_bit runs twice with same parameters... Unless the compiler is smart
enough to detect this and (re-)use the intermediate result later. But I haven't check
if this is the case with any current, old or future compilers. Has anyone?

Anyway, Sure.

Quoted 1 line
Quoted 9 lines
Quoted 1 line

Thanks, Chrustian. So, how is that supposed to be merged? I can move it with bitmap-for-next, unless there's no better branch.

Thanks,
Yury

Yury, did you take this?
If not, I'll take it through the ath tree.

author
#4 lore raw
Quoted 35 lines · Jeff Johnson
Quoted 34 lines · Yury Norov
Quoted 23 lines · Christian Lamparter

Hi,

Quoted 5 lines · Yury Norov

The function calls bitmap_empty() just before find_first_bit(). Both functions are O(N). Because find_first_bit() returns >= nbits in case of empty bitmap, the bitmap_empty() test may be avoided.

I looked up bitmap_empty():
<https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/bitmap.h#n423>

apart from the small_const_nbits stuff (which carl9170 likely does not qualify for since from what I remember it's a 128bits bitmap) the function just does:

| return find_first_bit(src, nbits) == nbits;

so yes, find_first_bit runs twice with same parameters... Unless the compiler is smart
enough to detect this and (re-)use the intermediate result later. But I haven't check
if this is the case with any current, old or future compilers. Has anyone?

Quoted 5 lines

Anyway, Sure.

Quoted 1 line

Thanks, Chrustian. So, how is that supposed to be merged? I can move it with bitmap-for-next, unless there's no better branch.

Quoted 6 lines
Quoted 3 lines

Thanks,
Yury

Yury, did you take this?
If not, I'll take it through the ath tree.

No. Please take with ath.

Applied
#5 lore raw
In reply to Yury Norov
Quoted 5 lines · Yury Norov

The function calls bitmap_empty() just before find_first_bit(). Both functions are O(N). Because find_first_bit() returns >= nbits in case of empty bitmap, the bitmap_empty() test may be avoided.

Applied, thanks!

[1/1] wifi: carl9170: micro-optimize carl9170_tx_shift_bm()

      commit: 08e3cc13b0d050860f41b4eaaa21c789af968b98

Best regards,

Signature · 1 line
Jeff Johnson <[email protected]>