Thread (26 messages) 26 messages, 3 authors, 2021-08-30

Re: [PATCH 11/17] find: micro-optimize for_each_{set,clear}_bit()

From: Petr Mladek <pmladek@suse.com>
Date: 2021-08-26 13:57:33
Also in: kvm, linux-arch, linux-kselftest, linux-mm, linux-mmc, lkml

On Sat 2021-08-14 14:17:07, Yury Norov wrote:
The macros iterate thru all set/clear bits in a bitmap. They search a
first bit using find_first_bit(), and the rest bits using find_next_bit().

Since find_next_bit() is called shortly after find_first_bit(), we can
save few lines of I-cache by not using find_first_bit().
Is this only a speculation or does it fix a real performance problem?

The macro is used like:

	for_each_set_bit(bit, addr, size) {
		fn(bit);
	}

IMHO, the micro-opimization does not help when fn() is non-trivial.

quoted hunk ↗ jump to hunk
--- a/include/linux/find.h
+++ b/include/linux/find.h
@@ -280,7 +280,7 @@ unsigned long find_next_bit_le(const void *addr, unsigned
 #endif
 
 #define for_each_set_bit(bit, addr, size) \
-	for ((bit) = find_first_bit((addr), (size));		\
+	for ((bit) = find_next_bit((addr), (size), 0);		\
 	     (bit) < (size);					\
 	     (bit) = find_next_bit((addr), (size), (bit) + 1))
 
It is not a big deal. I just think that the original code is slightly
more self-explaining.

Best Regards,
Petr
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help