summaryrefslogtreecommitdiffstats
path: root/libc/stdlib
diff options
context:
space:
mode:
authorAndré Goddard Rosa <andre.goddard@gmail.com>2010-03-28 21:18:56 -0300
committerAndré Goddard Rosa <andre.goddard@gmail.com>2010-03-28 21:18:56 -0300
commitacdb052030d6e537e51d8b42e07b2a9e71e6ee42 (patch)
treec42e974950bbe7d89eac29f62381d8d7e759f69b /libc/stdlib
parent4ad72f89b12fe0157074a253190f6aa2cbfe15ac (diff)
downloadbionic-acdb052030d6e537e51d8b42e07b2a9e71e6ee42.zip
bionic-acdb052030d6e537e51d8b42e07b2a9e71e6ee42.tar.gz
bionic-acdb052030d6e537e51d8b42e07b2a9e71e6ee42.tar.bz2
stdlib: optimize bsearch()
... by checking most probable condition first (elements do differ) Change-Id: I424eab9c32a6d9eb82b686ca04025ec8c9097035 Signed-off-by: André Goddard Rosa <andre.goddard@gmail.com>
Diffstat (limited to 'libc/stdlib')
-rw-r--r--libc/stdlib/bsearch.c4
1 files changed, 2 insertions, 2 deletions
diff --git a/libc/stdlib/bsearch.c b/libc/stdlib/bsearch.c
index 8193d27..7eb6325 100644
--- a/libc/stdlib/bsearch.c
+++ b/libc/stdlib/bsearch.c
@@ -56,11 +56,11 @@ bsearch(const void *key, const void *base0, size_t nmemb, size_t size,
for (lim = nmemb; lim != 0; lim >>= 1) {
p = base + (lim >> 1) * size;
cmp = (*compar)(key, p);
- if (cmp == 0)
- return ((void *)p);
if (cmp > 0) { /* key > p: move right */
base = (char *)p + size;
lim--;
+ } else if (cmp == 0) {
+ return ((void *)p);
} /* else move left */
}
return (NULL);