Wireshark-dev: [Wireshark-dev] strlen()
From: "Maynard, Chris" <[email protected]>
Date: Mon, 10 Aug 2009 22:10:33 -0400
I've noticed that there are several places where strings are being
checked if they're empty or not by using strlen().  Might it be faster
to simply check that the 1st byte of the string is non-zero instead of
having to traverse the entire string to compute the length?  If a macro
such as the following were to be used, it should not only be faster, but
clearer too.

#define str_empty(s)                (!(*(char *)(s)))

Then, for example, the following could be changed from:
capture.c:715:        if (strlen(if_parts[1]) > 0)
to:
capture.c:715:        if (!str_empty(if_parts[1]))

I haven't gone through the pains of finding and replacing all such
instances to see if there's any performance improvements or not, but in
theory it should help, especially if there are occurrences in critical
paths.  No?

There are other seemingly inefficient uses of strlen() as well where
it's called repeatedly instead of computing it once and using the return
value in subsequent comparisons.  For example:
pcapio.c:283:   if ((strlen(appname) > 0) && (strlen(appname) <
G_MAXUINT16)) {
Or maybe the compilers are all smart enough not to recompute the string
length more than once?

- Chris

CONFIDENTIALITY NOTICE: The contents of this email are confidential
and for the exclusive use of the intended recipient. If you receive this
email in error, please delete it from your system immediately and 
notify us either by email, telephone or fax. You should not copy,
forward, or otherwise disclose the content of the email.