Subject: Re: CVS commit: src/distrib/utils/sysinst
To: None <christos@zoulas.com>
From: None <cgd@broadcom.com>
List: source-changes
Date: 07/18/2003 13:02:29
At Fri, 18 Jul 2003 19:45:16 +0000 (UTC), christos@zoulas.com wrote:
> In article <yov5smp3ak37.fsf@ldt-sj3-010.sj.broadcom.com>,
>  <cgd@broadcom.com> wrote:
> >At Fri, 18 Jul 2003 15:28:07 +0000 (UTC), "John Hawkinson" wrote:
> >> Perry E. Metzger <perry@piermont.com> wrote on Fri, 18 Jul 2003
> >> at 10:21:31 -0400 in <87n0fbx6dw.fsf@snark.piermont.com>:
> >> 
> >> > David Laight <dsl@netbsd.org> writes:
> >> > > Change a lot of 'if (strlen(x) == 0)' to 'if (x[0] === 0)'.
> >> > 
> >> > Why bother? Was this performance critical code? The former is clearer
> >> > to the reader.
> >> 
> >> I do not think you can seriously argue that the clarity is stronger
> >> either way.
> >
> >I'd disagree: the former includes some semantic meaning that the
> >latter does not.
> 
> So what? Do you know what the programmer's intention was?

No, but the point is, if you go changing something written to express
the intent of 'take action on a string', to something like (x[0] ==
0), then you do lose meaning.

semantically speaking, "string length is 0" (or "string compares
equivalently to empty string") *does* provide more information about
the action being taken than "check that value at array index 0 is 0."

It's stupid to lose information if it doesn't actually buy you
anything.

(where IMO "buy you anything" should be measured with a modern version
of GCC.  There are other gains in compiled code to be had by
upgrading, and it's really lame that NetBSD ships a so-old version.
Saying "it helps some ancient compiler by a little bit" is really
pointless, when you can get all the same benefit and more by using a
newer and better compiler.)



> >the former says "check the length of the string 'x' against 0".
> >
> >the latter says "does the first byte of the memory pointed to by 'x'
> >happen to be 0?"
> 
> And it will take a moron a mental leap to go from:

"moron" or no, why make people think extra hard when they're trying to
read code?


For instance, without seeing more context, how do you even know that
'x' is even a "char *"?

(if you passed it to strcmp/strlen and it wasn't, you'd get a warning
at least.)

(what if you're using warnings to, say, help you convert code to wide
character strings?)

Again, why lose information when there's no gain in it?


> I would think that converting the strcmp(x, "") would be easier to optimize.

they *should* all come out the same.

I'd be a bit surprised if they didn't (using current GCC), but not
completely surprised.  If they don't, that's an obvious way to improve
the compiler.  8-)


cgd
-- 
Chris Demetriou                                            Broadcom Corporation
                Principal Design Engineer, Broadband Processors
  Any opinions expressed in this message are mine, not necessarily Broadcom's.