This is the mail archive of the gcc-bugs@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]

[Bug tree-optimization/81772] Compile-time snprintf optimization


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81772

--- Comment #2 from Daniel Fruzynski <bugzilla@poradnik-webmastera.com> ---
You probably could optimize snprintf(buf, size, "%s", str) into this:

if (size > 0)
{
  buf[0] = 0;
  strncat(buf, str, size - 1);
}

Other optimizations would require to generate more code aa you wrote. Some of
them probably could be applied, e.g. transforming first line to second:

sprintf(buf, "%s %d", "123", arg);
memcpy(buf, "123 ", 4); sprintf(buf+4, "%d", arg);

Dedicated string concatenation function with multiple args also would be
helpful to perform following transformation:

sprintf(buf, "Content-Type: %s/%s\n", str1, str2);
multistrcat(buf, "Content-Type: ", str1, "/", str2, "\n");

It is also possible to parse format string and create sprintf with series of
commands which will create the same result. Of course this will increase code
size like you wrote, so user would have to explicitly enable this optimization.
There are many factors there which must be taken into account, e.g. number of
format arguments, use of advanced formatting (e.g. setting field width or
precision), etc.

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