This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug tree-optimization/81772] Compile-time snprintf optimization
- From: "bugzilla at poradnik-webmastera dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 08 Aug 2017 20:49:03 +0000
- Subject: [Bug tree-optimization/81772] Compile-time snprintf optimization
- Auto-submitted: auto-generated
- References: <bug-81772-4@http.gcc.gnu.org/bugzilla/>
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.