|
|
|
|
|
|
|
|
|
|
xen-devel
RE: [Xen-devel] [PATCH] Cleanup use of strlen() to check for empty strin
> -----Original Message-----
> From: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
> [mailto:xen-devel-bounces@xxxxxxxxxxxxxxxxxxx] On Behalf Of
> Jacob Gorm Hansen
> Sent: 05 October 2005 11:45
> To: Keir Fraser; xen-devel@xxxxxxxxxxxxxxxxxxx
> Subject: Re: [Xen-devel] [PATCH] Cleanup use of strlen() to
> check for empty string
>
> On 10/5/05, Keir Fraser <Keir.Fraser@xxxxxxxxxxxx> wrote:
> >
> > On 4 Oct 2005, at 23:50, Jacob Gorm Hansen wrote:
> >
> > > I came across checks for strlen(s)==0 a few places in drivers/xen.
> > > Here is a patch to fix that.
> >
> > Isn't your patch exactly equivalent? None of that code is time
> > critical enough that a call to strlen matters.
>
> It's just gross (looping over the entire length of a string
> just to see if the first char is empty) that's all. Apply if you will.
I can see the argument both ways. A statement containing (strlen(s) ==
0) is very obvious what the meaning is. The compiler may even optimise
it into the form you've done (if it's clever enough).
On the other hand, if the compiler doesn't understand that (strlen(s) ==
0) is the same as (*s == 0), the code you've supplied would generate
shorter code (and run faster), which considering the number of calls to
strlen in the patch would give a decent benefit in code-size, and since
the code isn't speed critical (according to Keir), size is of more
benefit than speed.
I created a function:
int foo(char *s)
{
if (strlen(s)) return 1; else return 0;
}
Compiled with gcc 4.0.0 as:
gcc -O3 -s x.c (or gcc -O2 ...)
Gives the following code:
foo:
xorl %eax, %eax
cmpb $(0), (%rdi)
sete %al
ret
So, assuming the compiler is still equally clever on more complex
varations of this code, it should end up with exactly the same code as
your patch.
So although you've tried to optimise the code, it actually ends up being
nothing gained, since the compiler already can figure out what you
ACTUALLY wanted...
Of course, other versions of gcc may not be as clever... :-(
--
Mats
>
> Jacob
>
> _______________________________________________
> Xen-devel mailing list
> Xen-devel@xxxxxxxxxxxxxxxxxxx
> http://lists.xensource.com/xen-devel
>
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel
|
<Prev in Thread] |
Current Thread |
[Next in Thread>
|
- RE: [Xen-devel] [PATCH] Cleanup use of strlen() to check for empty string,
Petersson, Mats <=
|
|
|
|
|