Václav Zeman | 8 Mar 2012 14:37
Picon
Gravatar

avoid calling strlen() twice in readlink()

Hi.

Here is a tiny patch to avoid calling strlen() twice in readlink().

ChangeLog:

2012-03-08  Václav Zeman  <vhaisman <at> gmail.com>

        * path.cc (readlink): Avoid calling strlen() twice.

--

-- 
VZ
--- path.cc	2012-03-07 18:10:44.000000000 +0100
+++ path.cc	2012-03-08 13:28:28.468266800 +0100
 <at>  <at>  -2782,7 +2783,8  <at>  <at>  readlink (const char *path, char *buf, s
       return -1;
     }

-  ssize_t len = min (buflen, strlen (pathbuf.get_win32 ()));
+  size_t pathbuf_len = strlen (pathbuf.get_win32 ());
+  size_t len = MIN (buflen, pathbuf_len);
   memcpy (buf, pathbuf.get_win32 (), len);

   /* errno set by symlink.check if error */
Corinna Vinschen | 8 Mar 2012 15:56
Favicon

Re: avoid calling strlen() twice in readlink()

On Mar  8 14:37, Václav Zeman wrote:
> Hi.
> 
> Here is a tiny patch to avoid calling strlen() twice in readlink().
> 
> ChangeLog:
> 
> 2012-03-08  Václav Zeman  <...>
> 
>         * path.cc (readlink): Avoid calling strlen() twice.

Thanks, applied.

Corinna

--

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat

Eric Blake | 8 Mar 2012 16:29
Picon
Favicon
Gravatar

Re: avoid calling strlen() twice in readlink()

On 03/08/2012 06:37 AM, Václav Zeman wrote:
> Hi.
> 
> Here is a tiny patch to avoid calling strlen() twice in readlink().
> 

>  
> -  ssize_t len = min (buflen, strlen (pathbuf.get_win32 ()));
> +  size_t pathbuf_len = strlen (pathbuf.get_win32 ());
> +  size_t len = MIN (buflen, pathbuf_len);
>    memcpy (buf, pathbuf.get_win32 (), len);

For that matter, is calling pathbuf.get_win32() twice worth factoring out?

--

-- 
Eric Blake   eblake <at> redhat.com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Corinna Vinschen | 8 Mar 2012 16:56
Favicon

Re: avoid calling strlen() twice in readlink()

On Mar  8 08:29, Eric Blake wrote:
> On 03/08/2012 06:37 AM, Václav Zeman wrote:
> > Hi.
> > 
> > Here is a tiny patch to avoid calling strlen() twice in readlink().
> > 
> 
> >  
> > -  ssize_t len = min (buflen, strlen (pathbuf.get_win32 ()));
> > +  size_t pathbuf_len = strlen (pathbuf.get_win32 ());
> > +  size_t len = MIN (buflen, pathbuf_len);
> >    memcpy (buf, pathbuf.get_win32 (), len);
> 
> For that matter, is calling pathbuf.get_win32() twice worth factoring out?

It's just a const char *pointer, and it's an inline method.  I'm pretty
sure the compiler will optimize this just fine.

Corinna

--

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat

Christian Franke | 8 Mar 2012 19:48
Picon
Favicon

Re: avoid calling strlen() twice in readlink()

Corinna Vinschen wrote:
> On Mar  8 08:29, Eric Blake wrote:
>> On 03/08/2012 06:37 AM, Václav Zeman wrote:
>>> Hi.
>>>
>>> Here is a tiny patch to avoid calling strlen() twice in readlink().
>>>
>>>
>>> -  ssize_t len = min (buflen, strlen (pathbuf.get_win32 ()));
>>> +  size_t pathbuf_len = strlen (pathbuf.get_win32 ());
>>> +  size_t len = MIN (buflen, pathbuf_len);
>>>     memcpy (buf, pathbuf.get_win32 (), len);
>> For that matter, is calling pathbuf.get_win32() twice worth factoring out?
> It's just a const char *pointer, and it's an inline method.  I'm pretty
> sure the compiler will optimize this just fine.
>
>

Yes - and it does ever more:
strlen() is one of the compiler builtins declared with a const attribute 
internally. Then gcc optimizes duplicate calls away.

Testcase:

$ cat opt.cc
#include <string.h>

class X {
   const char * p;
   public:
(Continue reading)

Václav Zeman | 9 Mar 2012 12:34
Picon
Gravatar

Re: avoid calling strlen() twice in readlink()

On 8 March 2012 19:48, Christian Franke <Christian.Franke <at> t-online.de> wrote:
> Corinna Vinschen wrote:
>>
>> On Mar  8 08:29, Eric Blake wrote:
>>>
>>> On 03/08/2012 06:37 AM, Václav Zeman wrote:
>>>>
>>>> Hi.
>>>>
>>>> Here is a tiny patch to avoid calling strlen() twice in readlink().
>>>>
>>>>
>>>> -  ssize_t len = min (buflen, strlen (pathbuf.get_win32 ()));
>>>> +  size_t pathbuf_len = strlen (pathbuf.get_win32 ());
>>>> +  size_t len = MIN (buflen, pathbuf_len);
>>>>    memcpy (buf, pathbuf.get_win32 (), len);
>>>
>>> For that matter, is calling pathbuf.get_win32() twice worth factoring
>>> out?
>>
>> It's just a const char *pointer, and it's an inline method.  I'm pretty
>> sure the compiler will optimize this just fine.
>>
>>
>
> Yes - and it does ever more:
> strlen() is one of the compiler builtins declared with a const attribute
> internally. Then gcc optimizes duplicate calls away.
>
> Testcase:
(Continue reading)


Gmane