avoid calling strlen() twice in readlink()

Václav Zeman vhaisman@gmail.com
Fri Mar 9 11:35:00 GMT 2012


On 8 March 2012 19:48, Christian Franke <Christian.Franke@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:
>
> $ cat opt.cc
> #include <string.h>
>
> class X {
>  const char * p;
>  public:
>    X();
>    const char * get() { return p; }
> };
>
> int f(X & x)
> {
>  int i = 0;
>  i += strlen(x.get());
>  i += strlen(x.get());
>  i += strlen(x.get());
>  i += strlen(x.get());
>  i += strlen(x.get());
>  return i;
> }
>
> int g(X & x)
> {
>  return 5 * strlen(x.get());
> }
>
>
> $ gcc -S -O2 -fomit-frame-pointer opt.cc
>
> $ cat opt.s | c++filt
> ...
> f(X&):
>        subl    $28, %esp
>        movl    32(%esp), %eax
>        movl    (%eax), %eax
>        movl    %eax, (%esp)
>        call    _strlen
>        addl    $28, %esp
>        leal    (%eax,%eax,4), %eax
>        ret
> ...
> g(X&):
>        subl    $28, %esp
>        movl    32(%esp), %eax
>        movl    (%eax), %eax
>        movl    %eax, (%esp)
>        call    _strlen
>        addl    $28, %esp
>        leal    (%eax,%eax,4), %eax
>        ret
>
> (interesting: With -O1 it uses an inline version of strlen, with -O2,3,...
> it doesn't)
>
>
> So this patch probably had no effect at all, sorry :-)
Heh, no problem. I really did not know GCC could do that these days.
The benefit seemed too obvious to let it pass :)

-- 
VZ



More information about the Cygwin-patches mailing list