From 3d55b9b277ced49a7f13a1080049cbf326ba70fc Mon Sep 17 00:00:00 2001 From: haotuo <380743811@qq.com> Date: Tue, 16 May 2023 19:23:55 +0800 Subject: [PATCH] Optimize getenv performance by using self str cmp implementation Issue: #I73MBP Test: libc test & benchmark test Signed-off-by: haotuo Change-Id: Ibf8667e46c5199e5b7c3039029efb5b9daacc836 --- musl_src.gni | 1 + porting/linux/user/src/env/getenv.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 porting/linux/user/src/env/getenv.c diff --git a/musl_src.gni b/musl_src.gni index 07b5e05e..50d5da0e 100644 --- a/musl_src.gni +++ b/musl_src.gni @@ -2180,6 +2180,7 @@ musl_src_porting_file = [ "src/sigchain/sigchain.c", "src/conf/legacy.c", "src/conf/sysconf.c", + "src/env/getenv.c", ] musl_inc_hook_files = [ diff --git a/porting/linux/user/src/env/getenv.c b/porting/linux/user/src/env/getenv.c new file mode 100644 index 00000000..d5fa6710 --- /dev/null +++ b/porting/linux/user/src/env/getenv.c @@ -0,0 +1,24 @@ +#include +#include +#include + +char *getenv(const char *name) +{ + if (name == NULL || __environ == NULL) + return 0; + size_t i, l = 0; + const char *np; + char **p, *ep; + for (; *(name + l) && *(name + l) != '='; ++l); + for (p = __environ; (ep = *p) != NULL; ++p) { + for (np = name, i = l; i && *ep; i--) { + if (*ep++ != *np++) { + break; + } + } + if (i == 0 && *ep++ == '=') { + return (ep); + } + } + return 0; +} -- GitLab