This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Optimize sprintf(buffer,"foo")


"Kaveh R. Ghazi" <ghazi@caip.rutgers.edu> writes:

>  > The performance of strcpy+strlen is better than that the equivalent
>  > sprintf on most platforms
> 
> It's also bigger, so the -Os folks will come knocking eventually. :-)

I think this is actually OK.  The only case where the strlen() will
actually be called is when you have

n = sprintf (result, "%s", foo);

for a 'foo' that can't be constant-folded.  But, in that case, it's
the difference between

strcpy (result, foo);
n = strlen (foo);

and the above sprintf, so you've saved a constant load, and usually a
constant load is about as many instructions as a function call.
Certainly on powerpc, it's the difference between

        mr r3,result
        lis r4,LC0@ha
        addis r4,r4,LC0@l
        mr r5,foo
        bl sprintf
and
        mr r3,result
        mr r4,foo
        bl strcpy
        mr r3,foo
        bl strlen

which is exactly the same length, assuming that 'foo' gets assigned to
some call-saved register, except that you've saved the space for the
actual string constant.

[Actually, you could just do

        mr r3,result
        mr r4,foo
        bl strcpy
        bl strlen

which would be even better, since strcpy will return 'result.]

> You probably want to check optimize_size and punt if the strlen isn't
> turned into a constant.

-- 
- Geoffrey Keating <geoffk@geoffk.org>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]