This is the mail archive of the 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: Preparsing sprintf format strings

Kaveh R. GHAZI wrote:
> On Mon, 8 Oct 2007, Heikki Linnakangas wrote:
>> sprintf(dest, "%d", arg1); -> a new function that does the same thing,
>> but without the overhead of parsing the format string. Like itoa on some
>> platforms. We could inline it as well. That would allow further
>> optimizations, if for example the compiler knows that arg1 is within a
>> certain range (do we do that kind of optimizations?)
>> sprintf(dest, "constant%...", args...) -> memcpy(dest, "constant", 8);
>> sprintf(dest+8, "%...", args...);
>> sprintf(dest, "%dconstant%...", args1, args...) -> sprintf(dest, "%d",
>> args1); memcpy(dest+X, "constant", 8); sprintf(dest+XX, "%...", args...);
> In my experience changing one library call into several creates code bloat
> more often than it improves runtime speed.  I think you should only do
> this when profile feedback indicates you've hit a heavily used spot in
> your code.

Note that the memcpy's will be simplified further. Point taken, though.
I think the tradeoff is similar to -finline-functions and
-funswitch-loops, that we do with -O3.

> E.g. should we turn printf("ab") into putchar('a'); putchar('b'); ?
> I'd say not, unless that code was a hot spot.
> One simplification I don't believe we do yet, that should always be a win,
> is turning:   sprintf (foo, "%c", bar);   into:    *foo = bar;

Don't forget the null terminator ;-). Yeah, that seems always worth
doing. In printf, we already turn that into putchar.

  Heikki Linnakangas

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