Tom Lane <tgl@sss.pgh.pa.us> writes:
> Neil Conway <neilc@samurai.com> writes:
> > This patches replaces a few more usages of strcpy() and sprintf() when
> > copying into a fixed-size buffer (in this case, a buffer of
> > NAMEDATALEN bytes). AFAICT nothing to worry about here, but worth
> > fixing anyway...
>
> I'm not that eager to replace every single sprintf with snprintf.
> Most of these are obviously safe and do not need the clutter and
> extra cycles...
Hmm, can't seem to see much performance difference:
#include <stdio.h>
int
main(void)
{
char buf[128];
int i;
memset(buf, 0, sizeof(buf));
for (i = 0; i < 1000000; i++)
{
snprintf(buf, sizeof(buf), "%d %d %d %d %d %d %d %d %d %d %d",
i, i + i, i, i + i, i, i + i, i, i + 5, i, i, i);
}
}
With snprintf():
./a.out 7.76s user 0.04s system 99% cpu 7.848 total
./a.out 7.77s user 0.03s system 99% cpu 7.846 total
./a.out 7.70s user 0.09s system 99% cpu 7.867 total
./a.out 7.76s user 0.02s system 99% cpu 7.841 total
./a.out 7.74s user 0.03s system 98% cpu 7.852 total
With sprintf():
./a.out 7.83s user 0.09s system 98% cpu 8.042 total
./a.out 7.89s user 0.04s system 96% cpu 8.247 total
./a.out 7.83s user 0.02s system 98% cpu 7.937 total
./a.out 7.87s user 0.01s system 98% cpu 7.972 total
./a.out 7.80s user 0.01s system 98% cpu 7.918 total
If you're going to make a case for a performance hit, I'd agree that's
a concern -- but I'd prefer some actual data over vague claims of
"extra cycles".
Cheers,
Neil
--
Neil Conway <neilc@samurai.com> || PGP Key ID: DB3C29FC