getenv: Move call to strlen to the branch it's used in.
diff mbox series

Message ID VI1PR09MB43048D77BC8FF58CE93EAB87F77D0@VI1PR09MB4304.eurprd09.prod.outlook.com
State New
Headers show
Series
  • getenv: Move call to strlen to the branch it's used in.
Related show

Commit Message

Lode Willems Nov. 2, 2019, 9:58 a.m. UTC
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.

Comments

Florian Weimer Nov. 2, 2019, 3:20 p.m. UTC | #1
* 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.
Lode Willems Nov. 2, 2019, 3:55 p.m. UTC | #2
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.

Patch
diff mbox series

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