Skip to content

Commit

Permalink
lib/bsearch: Provide __always_inline variant
Browse files Browse the repository at this point in the history
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().

[ tglx: Renamed to __inline_bsearch() as suggested by Andy ]

Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Signed-off-by: Thomas Gleixner <[email protected]>
Reviewed-by: Alexandre Chartre <[email protected]>
Acked-by: Andy Lutomirski <[email protected]>
Link: https://lkml.kernel.org/r/[email protected]
  • Loading branch information
Peter Zijlstra authored and KAGA-KOKO committed Jun 11, 2020
1 parent ef882bf commit df65bba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
26 changes: 24 additions & 2 deletions include/linux/bsearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,29 @@

#include <linux/types.h>

void *bsearch(const void *key, const void *base, size_t num, size_t size,
cmp_func_t cmp);
static __always_inline
void *__inline_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 */
22 changes: 2 additions & 20 deletions lib/bsearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 __inline_bsearch(key, base, num, size, cmp);
}
EXPORT_SYMBOL(bsearch);
NOKPROBE_SYMBOL(bsearch);

0 comments on commit df65bba

Please sign in to comment.