From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oa1-x2a.google.com (mail-oa1-x2a.google.com [IPv6:2001:4860:4864:20::2a]) by sourceware.org (Postfix) with ESMTPS id 827E638493D0 for ; Thu, 16 Feb 2023 13:49:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 827E638493D0 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-oa1-x2a.google.com with SMTP id 586e51a60fabf-16dcb07b805so2650972fac.0 for ; Thu, 16 Feb 2023 05:49:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=sLJ9yMiy8qkTod9EDsjOqsOCrvIMNqC3FJpfCn60o7c=; b=dkQViejIumr72eApMjojNCpQ2TxjK2SOYd/5f5kNWZVazIMwiP+BFacnl9xRNgrIpY yA8d50EjQWZpi2TW1CdOeieZfZQvCpdbpMz2E7selOuSXaRckNL+BJP+IHjJBbH9duB1 vBhn/uROzMpiiVmfYagiSbNvMERR3A824kaGWhLRIl0XbN1jliJtVWLLHVzfh7GU++x2 2KtFBz+5k0S0PjA8STIRWOOfe9Hj031RGHMT/L9F0AsMUPg3qZ+A8WC6HxGdyG7n3M1D vHZnDkWguSqiiiXutNjHp5gp0ZiSuh+el+gowGMELwJX8U2pYwe6YCGb/pctshqLqfBi ZOGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sLJ9yMiy8qkTod9EDsjOqsOCrvIMNqC3FJpfCn60o7c=; b=QlSPg3LuxKiDXuBzUQwmdf2JC0sH/uGiPZbwdYymZGRTyJP7/eHTANVlVeE5AGyEmu ELY+CuMlactaX05Ux3vPg919VShh5gcHLC6mlQ8Q0Qdi3LqQkSv5jP96XiPFonUurBJC YlZRV9blHNF/6JlH3Uaq8trqgCmVKXg1zkBqFGbs5ZWFPD8c6w6k1GellnNkza9kjQ96 64tEkGIah/VaeCXP4U0WqQpQdSYKCh6XUsvpWInbC9wUrMuonSPVK0pPj+LERbgXL8IE PVEWRR5G7SBXIzS7MI+KMDHtWZPLyb5eyHXkNQ3yg/gBinOyo63iRcKLDvwfJvQs4S9W droQ== X-Gm-Message-State: AO0yUKUnreJNIgPF/b2Nea5GnbNUXxBt0CSjzsfwJqecaTg07TE963Xg BizFQoOHSf4Wf1cUeKWAQAAj9U9qbJi5lOQ8lrI= X-Google-Smtp-Source: AK7set9av6O5cgxo11QZr4zCp/k7+RQ/8nRwksRqY91B/M9WptWKb5ZzxJO82AJuNgJ0rf/QNhVSNA== X-Received: by 2002:a05:6870:f151:b0:16e:84c5:7496 with SMTP id l17-20020a056870f15100b0016e84c57496mr382732oac.11.1676555395554; Thu, 16 Feb 2023 05:49:55 -0800 (PST) Received: from ?IPV6:2804:1b3:a7c3:3a5:d16:437b:3b74:d999? ([2804:1b3:a7c3:3a5:d16:437b:3b74:d999]) by smtp.gmail.com with ESMTPSA id bf24-20020a0568700a1800b00150aca072e8sm565742oac.38.2023.02.16.05.49.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 16 Feb 2023 05:49:54 -0800 (PST) Message-ID: <7d887c0c-7ab0-4057-df39-e8d671bca1d0@linaro.org> Date: Thu, 16 Feb 2023 10:49:52 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.7.2 Subject: Re: [PATCH 2/7] stdlib: Simplify getenv Content-Language: en-US To: Wilco Dijkstra , "libc-alpha@sourceware.org" References: <20230213135558.3328727-1-adhemerval.zanella@linaro.org> <20230213135558.3328727-3-adhemerval.zanella@linaro.org> From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00,BODY_8BITS,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 15/02/23 14:50, Wilco Dijkstra wrote: > Hi Adhemerval, > > +  size_t len = __strchrnul (name, '=') - name; > > This is a change in behaviour, ignoring anything behind a '=' in name. What is > wrong with strlen? Indeed it is better to keep current semantic. > > +  for (char **ep = __environ; *ep != NULL; ++ep) >      { > -      size_t len = strlen (name); > -#if _STRING_ARCH_unaligned > -      name_start = *(const uint16_t *) name; > -#else > -      name_start = (((const unsigned char *) name)[0] > -                   | (((const unsigned char *) name)[1] << 8)); > -#endif > -      len -= 2; > -      name += 2; > - > -      for (ep = __environ; *ep != NULL; ++ep) > -       { > -#if _STRING_ARCH_unaligned > -         uint16_t ep_start = *(uint16_t *) *ep; > -#else > -         uint16_t ep_start = (((unsigned char *) *ep)[0] > -                              | (((unsigned char *) *ep)[1] << 8)); > -#endif > - > -         if (name_start == ep_start && !strncmp (*ep + 2, name, len) > -             && (*ep)[len + 2] == '=') > -           return &(*ep)[len + 3]; > -       } > +      if (strncmp (name, *ep, len) == 0 && (*ep)[len] == '=') > +       return *ep + len + 1; > > It's still useful to keep a check for the first character, eg. name[0] == (*ep)[0] as this > is about twice as fast as always calling strncmp. Alright, updated patch below. >From ddfb6419c5e74b34fb22454581fa0636275ec942 Mon Sep 17 00:00:00 2001 From: Adhemerval Zanella Date: Thu, 9 Feb 2023 10:36:57 -0300 Subject: [PATCH 2/7] stdlib: Simplify getenv And remove _STRING_ARCH_unaligned usage. Checked on x86_64-linux-gnu and i686-linux-gnu. --- stdlib/getenv.c | 68 +++++++------------------------------------------ 1 file changed, 9 insertions(+), 59 deletions(-) diff --git a/stdlib/getenv.c b/stdlib/getenv.c index e3157ce2f3..61a42d941d 100644 --- a/stdlib/getenv.c +++ b/stdlib/getenv.c @@ -15,76 +15,26 @@ License along with the GNU C Library; if not, see . */ -#include -#include -#include +#include #include #include #include - -/* Return the value of the environment variable NAME. This implementation - is tuned a bit in that it assumes no environment variable has an empty - name which of course should always be true. We have a special case for - one character names so that for the general case we can assume at least - two characters which we can access. By doing this we can avoid using the - `strncmp' most of the time. */ char * getenv (const char *name) { - char **ep; - uint16_t name_start; - if (__environ == NULL || name[0] == '\0') return NULL; - if (name[1] == '\0') - { - /* The name of the variable consists of only one character. Therefore - the first two characters of the environment entry are this character - and a '=' character. */ -#if __BYTE_ORDER == __LITTLE_ENDIAN || !_STRING_ARCH_unaligned - name_start = ('=' << 8) | *(const unsigned char *) name; -#else - name_start = '=' | ((*(const unsigned char *) name) << 8); -#endif - for (ep = __environ; *ep != NULL; ++ep) - { -#if _STRING_ARCH_unaligned - uint16_t ep_start = *(uint16_t *) *ep; -#else - uint16_t ep_start = (((unsigned char *) *ep)[0] - | (((unsigned char *) *ep)[1] << 8)); -#endif - if (name_start == ep_start) - return &(*ep)[2]; - } - } - else - { - size_t len = strlen (name); -#if _STRING_ARCH_unaligned - name_start = *(const uint16_t *) name; -#else - name_start = (((const unsigned char *) name)[0] - | (((const unsigned char *) name)[1] << 8)); -#endif - len -= 2; - name += 2; - - for (ep = __environ; *ep != NULL; ++ep) - { -#if _STRING_ARCH_unaligned - uint16_t ep_start = *(uint16_t *) *ep; -#else - uint16_t ep_start = (((unsigned char *) *ep)[0] - | (((unsigned char *) *ep)[1] << 8)); -#endif + bool single_char = name[1] == '\0'; - if (name_start == ep_start && !strncmp (*ep + 2, name, len) - && (*ep)[len + 2] == '=') - return &(*ep)[len + 3]; - } + 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; } return NULL; -- 2.34.1