1 Star 0 Fork 16

ocs-commit-check/glibc

forked from OpenCloudOS Stream/glibc 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
950891b5e7a5307272da3e632832ac9da4c9eeec.patch 1.84 KB
一键复制 编辑 原始数据 按行查看 历史
ocs-bot 提交于 2025-01-02 17:01 . - [Type] bugfix
From 950891b5e7a5307272da3e632832ac9da4c9eeec Mon Sep 17 00:00:00 2001
From: Kuan-Wei Chiu <[email protected]>
Date: Tue, 10 Sep 2024 00:23:27 +0800
Subject: [PATCH] Optimize bsearch() implementation for performance
Optimize the bsearch() function to improve binary search performance.
Although the code size grew by 8 bytes, the new implementation achieves
a 15% reduction in execution time on my x86 machine, according to the
bench-bsearch benchmark results.
Signed-off-by: Kuan-Wei Chiu <[email protected]>
Reviewed-by: Noah Goldstein <[email protected]>
Reviewed-by: DJ Delorie <[email protected]>
---
bits/stdlib-bsearch.h | 20 +++++++++-----------
1 file changed, 9 insertions(+), 11 deletions(-)
diff --git a/bits/stdlib-bsearch.h b/bits/stdlib-bsearch.h
index 540d718952..57f060b504 100644
--- a/bits/stdlib-bsearch.h
+++ b/bits/stdlib-bsearch.h
@@ -20,22 +20,14 @@ __extern_inline void *
bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size,
__compar_fn_t __compar)
{
- size_t __l, __u, __idx;
const void *__p;
int __comparison;
- __l = 0;
- __u = __nmemb;
- while (__l < __u)
+ while (__nmemb)
{
- __idx = (__l + __u) / 2;
- __p = (const void *) (((const char *) __base) + (__idx * __size));
+ __p = (const void *) (((const char *) __base) + ((__nmemb >> 1) * __size));
__comparison = (*__compar) (__key, __p);
- if (__comparison < 0)
- __u = __idx;
- else if (__comparison > 0)
- __l = __idx + 1;
- else
+ if (__comparison == 0)
{
#if __GNUC_PREREQ(4, 6)
# pragma GCC diagnostic push
@@ -46,6 +38,12 @@ bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size,
# pragma GCC diagnostic pop
#endif
}
+ if (__comparison > 0)
+ {
+ __base = ((const char *) __p) + __size;
+ --__nmemb;
+ }
+ __nmemb >>= 1;
}
return NULL;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ocs-commit-check/glibc.git
[email protected]:ocs-commit-check/glibc.git
ocs-commit-check
glibc
glibc
master

搜索帮助