[PATCH 2/7] stdlib: Simplify getenv

Wilco Dijkstra Wilco.Dijkstra@arm.com
Thu Feb 16 18:02:07 GMT 2023


Hi Adhemerval,

+  size_t len = strlen (name);;
+  for (char **ep = __environ; *ep != NULL; ++ep)
+    {
+      if (single_char && (*ep)[0] == name[0] && (*ep)[1] == '=')
+       return *ep + 2;
+      else if (strncmp (name, *ep, len) == 0 && (*ep)[len] == '=')
+       return *ep + len + 1;

This is about 10-20% slower than the previous patch both for single-char and
multi-char case... The approach I showed it is far simpler and 3-4 times faster
(within 10% of original performance).

Cheers,
Wilco


More information about the Libc-alpha mailing list