Adventures with gcc: code vs object-code size

Peter Jeremy peterjeremy at optushome.com.au
Sun Mar 21 00:46:09 PST 2004


On Sun, Mar 21, 2004 at 02:20:13AM -0500, Matt Emmerton wrote:
>
>----- Original Message ----- 
>From: "Garance A Drosihn" <drosih at rpi.edu>
>To: <hackers at freebsd.org>
>Sent: Saturday, March 20, 2004 5:45 PM
>Subject: Adventures with gcc: code vs object-code size
>> if (strcmp(elemcopy, ":") == 0)
...
>I don't know why the code bloats so much on i386, but I do question the use
>of strcmp() for a single-character compare.
>Something like the following would probably be better (and would avoid your
>problem):
>
>if (elemcopy[0] == ':')
>  inf->count = 0;
>else
>  inf->addelem(inf, elemcopy);

That code isn't equivalent in general.  Your code just checks that the
first character is ':'.  It doesn't verify that the string is exactly
one character long.  It's possible that in this particular context,
elemcopy may be constrained such that it must be ":" if the first
character is ':' but this isn't clear from Garances posting.  The
equivalent code would be:

if (elemcopy[0] == ':' && elemcopy[1] == '\0')
  inf->count = 0;
else
  inf->addelem(inf, elemcopy);

But (IMHO) this is a lot less clear than the former code (thought I admit
I'm guilty of doing this quite a lot in my code).  Note that a modern C
compiler is free to convert
  strcpy(elemcopy, ":") == 0
into
  elemcopy[0] == ':' && elemcopy[1] == '\0'
assuming the relevant header (<string.h>) is in scope.  (I was under the
impression that gcc did this).

Peter


More information about the freebsd-hackers mailing list