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

2021-09-05 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81772

Andrew Pinski  changed:

   What|Removed |Added

   Severity|normal  |enhancement

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

2019-05-24 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81772

Martin Sebor  changed:

   What|Removed |Added

   See Also||https://gcc.gnu.org/bugzill
   ||a/show_bug.cgi?id=88814

--- Comment #4 from Martin Sebor  ---
I forgot about this request.  An alternate solution to the strncat trick is to
use memccpy (bug 88814).  I'm hoping to look into one or the other for GCC 10.

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

2017-08-08 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81772

Martin Sebor  changed:

   What|Removed |Added

 Status|UNCONFIRMED |NEW
   Last reconfirmed||2017-08-09
 Ever confirmed|0   |1

--- Comment #3 from Martin Sebor  ---
Thanks for the strncat suggestion.  That seems quite doable.  I'll try to find
some time to look into it.

I explored some of the other ideas when I implemented the GCC 7 sprintf
optimization (-fprintf-return-value) and warnings (-Wformat-overflow and
-Wformat-truncation) but they didn't seem applicable in enough situations (when
all or at least some of the arguments were constant) to justify either the
additional complexity or the space overhead.

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

2017-08-08 Thread bugzi...@poradnik-webmastera.com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81772

--- Comment #2 from Daniel Fruzynski  ---
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.

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

2017-08-08 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81772

Martin Sebor  changed:

   What|Removed |Added

 CC||msebor at gcc dot gnu.org

--- Comment #1 from Martin Sebor  ---
GCC 7 does implement some additional sprintf and snprintf optimizations but it
doesn't go quite as far as you'd like it to.  The trouble is that there is no
suitable alternative function to call when the arguments aren't constant.  I
suppose in the non-constant case and for small enough positive n, snprintf(d,
n, "%s", s) could be transformed into something like

  tmp = strlen (s);
  if (n <= tmp)
tmp = n - 1;
  memcpy (d, s, tmp);
  d[tmp] = '\0';

trading off space (more code) for speed.  But whether or not this would be a
win would depend on the snprintf implementation.  It seems to me that a better
place to implement this optimization is in snprintf itself.