Preparsing sprintf format strings

Paolo Bonzini bonzini@gnu.org
Mon Oct 8 16:08:00 GMT 2007


>> It's too far-fetched for my tastes. I think gcc should not do it.
>> How gcc can know what printf() and puts() mean in *my* libc?
> 
> Well, that's what -fno-builtin is for.

More precisely, -ffreestanding.

>> I think such optimizations should be done in glibc.
> 
> The point is that you can't. GCC knows if an argument is a string
> literal 

... not only because of that.  The point is that only a compiler can 
parse the format string upfront.  GCC can look at the format string of this

   char *x;
   ...
   printf ("foo %s bar", x);

and compile it as

   fp = stdout;
   fputs ("foo ", fp);
   fputs (x, fp);
   fputs (" bar", fp);

while glibc must obviously parse the format string at run-time (which is 
the main source of overhead).

>>> sprintf(dest, "%dconstant%...", args1, args...) -> sprintf(dest, "%d",
>>> args1); memcpy(dest+X, "constant", 8); sprintf(dest+XX, "%...", args...);
>> How do you know that in this place user wants faster, not smaller code?
> 
> I suppose we would only do the transformations that have a speed-space
> tradeoff with -O3, and not with -Os.

You could also make it profile-driven.

Paolo



More information about the Gcc mailing list