This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
glibc calls more write() than necessary
- From: Matthieu Lemerre <racin at free dot fr>
- To: libc-alpha at sources dot redhat dot com
- Date: Sun, 21 Jan 2007 21:08:38 +0100
- Subject: glibc calls more write() than necessary
Hello,
The following testcases on Linux 2.6.18 glibc 2.5 (or 2.3)
int
main (int argc, char **argv)
{
printf ("hello\nworld\ne");
printf ("hello\n\%s\n","char");
}
yields (with strace)
write(1, "hello\n", 6hello
) = 6
write(1, "world\n", 6world
) = 6
write(1, "ehello\n", 7ehello
) = 7
write(1, "char\n", 5char
It seems to me that only 2 writes are really necessary in a line
buffered stream; and that the libc has means to know that (recording
where the last '\n' does not seem overly complicated from an outside
point of view).
Maybe am I missing something; maybe POSIX mandates this behaviour, or
maybe I overestimate the cost of a syscall on Linux.
Maybe the arrangement of function calls does not make this possible
without parsing the string to send; but line-buffered outputs have to
parse the string for the presence of '\n' anyway.
Another test with:
puts ("Toto\nworld\n");
yields
write(1, "Toto\nworld\n", 11Toto
world
) = 11
write(1, "\n", 1
) = 1
Which is more understandable: I guess this avoids copying the string
by using it directly as an argument to the syscall.
So my final guess is that implementing my "optimisation" would prevent
some code factoring in libc, and is finally not worth it.
Regards,
Matthieu Lemerre
PS: I originally wanted to write a small patch for this, but as the
libc is quite huge, entering it would take me too much time. I
apologize about that.