[patch] ld: optimize vfinfo output slightly

Mike Frysinger vapier@gentoo.org
Sat Mar 24 05:22:00 GMT 2012


ld atm ends up calling the write() syscall on every char when displaying an 
error message.  for example:
$ echo 'main(){foo();}' | strace -f -ewrite gcc -x c -o /dev/null -
...
[pid 13035] write(2, ":", 1)            = 1
[pid 13035] write(2, " ", 1)            = 1
[pid 13035] write(2, "I", 1)            = 1
[pid 13035] write(2, "n", 1)            = 1
[pid 13035] write(2, " ", 1)            = 1
[pid 13035] write(2, "f", 1)            = 1
[pid 13035] write(2, "u", 1)            = 1
[pid 13035] write(2, "n", 1)            = 1
[pid 13035] write(2, "c", 1)            = 1
[pid 13035] write(2, "t", 1)            = 1
[pid 13035] write(2, "i", 1)            = 1
[pid 13035] write(2, "o", 1)            = 1
[pid 13035] write(2, "n", 1)            = 1
[pid 13035] write(2, " ", 1)            = 1
[pid 13035] write(2, "`", 1)            = 1
...

that's just to write ": In function `main':".  a slight optimization in the 
vfinfo() func gives a much more reasonable syscall footprint:
...
write(2, ": In function `", 15)         = 15
...

it's been a while since i've hacked in these layers, so i'm not sure if 
there's something better than just calling abort() here ...
-mike

2012-03-24  Mike Frysinger  <vapier@gentoo.org>

	* ldmisc.c (vfinfo): Assign new local str to fmt.  Delete
	putc call.  If str and fmt are different, call fwrite on
	the difference.

--- ldmisc.c	18 Feb 2012 11:55:45 -0000	1.42
+++ ldmisc.c	24 Mar 2012 05:13:24 -0000
@@ -72,10 +72,15 @@ vfinfo (FILE *fp, const char *fmt, va_li
 
   while (*fmt != '\0')
     {
+      const char *str = fmt;
       while (*fmt != '%' && *fmt != '\0')
+	fmt++;
+      if (fmt != str)
 	{
-	  putc (*fmt, fp);
-	  fmt++;
+	  size_t len = fmt - str;
+
+	  if (fwrite (str, 1, len, fp) != len)
+	    abort ();
 	}
 
       if (*fmt == '%')
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <https://sourceware.org/pipermail/binutils/attachments/20120324/710d690f/attachment.sig>


More information about the Binutils mailing list