Re: [patch V4 part 4 03/24] lib/bsearch: Provide __always_inline variant

2020-05-13 Thread Andy Lutomirski
On Tue, May 5, 2020 at 7:15 AM Thomas Gleixner  wrote:
>
> From: Peter Zijlstra 
>
> For code that needs the ultimate performance (it can inline the @cmp
> function too) or simply needs to avoid calling external functions for
> whatever reason, provide an __always_inline variant of bsearch().


Acked-by: Andy Lutomirski 

Although maybe a more explicit name (e.g. __inlined_bsearch()) would
be more clear?


[patch V4 part 4 03/24] lib/bsearch: Provide __always_inline variant

2020-05-05 Thread Thomas Gleixner
From: Peter Zijlstra 

For code that needs the ultimate performance (it can inline the @cmp
function too) or simply needs to avoid calling external functions for
whatever reason, provide an __always_inline variant of bsearch().

Signed-off-by: Peter Zijlstra (Intel) 
Signed-off-by: Thomas Gleixner 
---
 include/linux/bsearch.h |   26 --
 lib/bsearch.c   |   22 ++
 2 files changed, 26 insertions(+), 22 deletions(-)

--- a/include/linux/bsearch.h
+++ b/include/linux/bsearch.h
@@ -4,7 +4,29 @@
 
 #include 
 
-void *bsearch(const void *key, const void *base, size_t num, size_t size,
- cmp_func_t cmp);
+static __always_inline
+void *__bsearch(const void *key, const void *base, size_t num, size_t size, 
cmp_func_t cmp)
+{
+   const char *pivot;
+   int result;
+
+   while (num > 0) {
+   pivot = base + (num >> 1) * size;
+   result = cmp(key, pivot);
+
+   if (result == 0)
+   return (void *)pivot;
+
+   if (result > 0) {
+   base = pivot + size;
+   num--;
+   }
+   num >>= 1;
+   }
+
+   return NULL;
+}
+
+extern void *bsearch(const void *key, const void *base, size_t num, size_t 
size, cmp_func_t cmp);
 
 #endif /* _LINUX_BSEARCH_H */
--- a/lib/bsearch.c
+++ b/lib/bsearch.c
@@ -28,27 +28,9 @@
  * the key and elements in the array are of the same type, you can use
  * the same comparison function for both sort() and bsearch().
  */
-void *bsearch(const void *key, const void *base, size_t num, size_t size,
- cmp_func_t cmp)
+void *bsearch(const void *key, const void *base, size_t num, size_t size, 
cmp_func_t cmp)
 {
-   const char *pivot;
-   int result;
-
-   while (num > 0) {
-   pivot = base + (num >> 1) * size;
-   result = cmp(key, pivot);
-
-   if (result == 0)
-   return (void *)pivot;
-
-   if (result > 0) {
-   base = pivot + size;
-   num--;
-   }
-   num >>= 1;
-   }
-
-   return NULL;
+   return __bsearch(key, base, num, size, cmp);
 }
 EXPORT_SYMBOL(bsearch);
 NOKPROBE_SYMBOL(bsearch);