[PATCH 2/7] stdlib: Simplify getenv

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Thu Feb 16 18:38:07 GMT 2023



On 16/02/23 15:02, Wilco Dijkstra wrote:
> 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).

Do you mean something like:

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


More information about the Libc-alpha mailing list