public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cpumask: Optimize cpumask_any_but()
@ 2025-01-17 14:26 Kuan-Wei Chiu
  2025-01-17 14:59 ` I Hsin Cheng
  0 siblings, 1 reply; 6+ messages in thread
From: Kuan-Wei Chiu @ 2025-01-17 14:26 UTC (permalink / raw)
  To: yury.norov
  Cc: linux, richard120310, jserv, mark.rutland, linux-kernel,
	Kuan-Wei Chiu, Yu-Chun Lin

The cpumask_any_but() function can avoid using a loop to determine the
CPU index to return. If the first set bit in the cpumask is not equal
to the specified CPU, we can directly return the index of the first set
bit. Otherwise, we return the next set bit's index.

This optimization replaces the loop with a single if statement,
allowing the compiler to generate more concise and efficient code.

As a result, the size of the bzImage built with x86 defconfig is
reduced by 4096 bytes:

* Before:
$ size arch/x86/boot/bzImage
   text    data     bss     dec     hex filename
13537280           1024       0 13538304         ce9400 arch/x86/boot/bzImage

* After:
$ size arch/x86/boot/bzImage
   text    data     bss     dec     hex filename
13533184           1024       0 13534208         ce8400 arch/x86/boot/bzImage

Co-developed-by: Yu-Chun Lin <eleanor15x@gmail.com>
Signed-off-by: Yu-Chun Lin <eleanor15x@gmail.com>
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
Not sure how to measure the efficiency difference, but I guess this
patch might be slightly more efficient or nearly the same as before. If
you have any good ideas for measuring efficiency, please let me know!

 include/linux/cpumask.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
index 9278a50d514f..b769fcdbaa10 100644
--- a/include/linux/cpumask.h
+++ b/include/linux/cpumask.h
@@ -404,10 +404,10 @@ unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
 	unsigned int i;
 
 	cpumask_check(cpu);
-	for_each_cpu(i, mask)
-		if (i != cpu)
-			break;
-	return i;
+	i = find_first_bit(cpumask_bits(mask), small_cpumask_bits);
+	if (i != cpu)
+		return i;
+	return find_next_bit(cpumask_bits(mask), small_cpumask_bits, i + 1);
 }
 
 /**
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-01-23 22:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-17 14:26 [PATCH] cpumask: Optimize cpumask_any_but() Kuan-Wei Chiu
2025-01-17 14:59 ` I Hsin Cheng
2025-01-17 16:32   ` Kuan-Wei Chiu
2025-01-17 16:32   ` Yury Norov
2025-01-18  7:32     ` Kuan-Wei Chiu
2025-01-23 22:39       ` Yury Norov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox