This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] getenv: Move call to strlen to the branch it's used in.
- From: Lode Willems <Lode dot Willems at UGent dot be>
- To: Florian Weimer <fw at deneb dot enyo dot de>
- Cc: "libc-alpha at sourceware dot org" <libc-alpha at sourceware dot org>
- Date: Sat, 2 Nov 2019 15:55:55 +0000
- Subject: Re: [PATCH] getenv: Move call to strlen to the branch it's used in.
- Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=ugent.be; dmarc=pass action=none header.from=ugent.be; dkim=pass header.d=ugent.be; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=xBs8AHn7o6erIShH1BnLirv/atTL0WvgPAbCKoe7sPg=; b=k8a0fbIfIcP1/X7XacKqyAEhI9zWkVDLMtEUa2K8hP3e6yhbtFKKm1qXaCVxzB7xBt/Y0IWZmwoyw8jPKGcHrUDWJdLnVnRLAUMknEX5gSLqVYM8T7/fv9MUojLRekRldH0ljJH17jzZE2nl8Z12aRv2if0Y1W0R3+KzUtobR40eIT/7UBkbzau4AmtU9Z+g3lTdQudAMtoNzP9wimOdJiLLeelWxkGNWF7g+jTXvwFwHAM4G2TpwAF29dhGkI9JByqdHXHeJnwGVkEte1izqWjpKW2eyhvhc5Fe3BxmdhFG9uG3k9x5n4/IxrzqgNuuNPj/eygLcyKe/Be0pfkRsA==
- Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gHK2628vu/dXFtfEjs7tvQha4g9FHG339V8Ep3hJdrrShOzl04wKz/pXtm3JQHNvep5yCrPuJhYOCirucYm6UTjKN62cjNDbKEoBWdyYZD8aw3RZ6DpfmF2oAtnNlh3YahROoBB45B9AsyqHwPq+j2GHqKr1e9jo0E31KLhjjsl9EQsvUhNanImzixxT/OxKxiK6i10o8ZN4/4rGnZ3GvLWIzKNyo03tEKP0MD55KsOJvLKp5fdyHDki9pbeTHzsYWudts+UYrfLo2lYnQ3TvHWG+nJU4FuxOr+/Z0zfN7Wm6WayVt9gdN0wptTtY5UjqfQVnDD6KxO09gRKwH4AoA==
- References: <VI1PR09MB43048D77BC8FF58CE93EAB87F77D0@VI1PR09MB4304.eurprd09.prod.outlook.com>,<87wociz4nn.fsf@mid.deneb.enyo.de>
In my testing it did alter the generated machine code (compiled with GCC
9.2 and -O3). The patched version is ever so slightly faster. And it
just seems the most logical for clarity.
From: Florian Weimer <fw@deneb.enyo.de>
Sent: Saturday, November 2, 2019 4:20 PM
To: Lode Willems <Lode.Willems@UGent.be>
Cc: libc-alpha@sourceware.org <libc-alpha@sourceware.org>
Subject: Re: [PATCH] getenv: Move call to strlen to the branch it's used in.
* Lode Willems:
> The len variable is only used in the else branch.
> We don't need the call to strlen if the name is 0 or 1 characters long.
>
> 2019-10-02 Lode Willems <Lode.Willems@UGent.be>
>
> * tdlib/getenv.c: Move the call to strlen into the branch it's used.
>
> diff --git a/stdlib/getenv.c b/stdlib/getenv.c
> index 6be97b2a54..aa5e69d0b0 100644
> --- a/stdlib/getenv.c
> +++ b/stdlib/getenv.c
> @@ -32,7 +32,6 @@
> char *
> getenv (const char *name)
> {
> - size_t len = strlen (name);
> char **ep;
> uint16_t name_start;
>
> @@ -63,6 +62,7 @@ getenv (const char *name)
> }
> else
> {
> + size_t len = strlen (name);
> #if _STRING_ARCH_unaligned
> name_start = *(const uint16_t *) name;
> #else
Thanks for the patch. Is there any particular reason for making the
change? I don't think it will alter the generated machine code.