2006-12-08 13:25:28

by Ulrich Windl

[permalink] [raw]
Subject: 2.6.19: slight performance optimization for lib/string.c's strstrip()

Hi,

my apologies for disobeying all the rules for submitting patches, but I'll suggest
a performance optimization for strstrip() in lib/string.c:

Original routine:
char *strstrip(char *s)
{
size_t size;
char *end;

size = strlen(s);

if (!size)
return s;

end = s + size - 1;
while (end >= s && isspace(*end))
end--;
*(end + 1) = '\0';

while (*s && isspace(*s))
s++;

return s;
}
EXPORT_SYMBOL(strstrip);


Suggested replacement:

char *strstrip(char *s)
{
size_t size;
char *end;

while (*s && isspace(*s))
s++;
if (!*s)
return s;
size = strlen(s);

end = s + size - 1;
while (end > s && isspace(*end))
end--;
*(end + 1) = '\0';

return s;
}
EXPORT_SYMBOL(strstrip);

Comments: There's no need to scan the initial banks at the start of the string
twice (using strlen(), and then looking for initial blanks), and we know that the
first character of the string cannot be a blank when we are removing trailing
blanks after having removed leading blanks. Also we do not need to call strlen()
to detect an empty string.

Regards,
Ulrich


2006-12-08 13:50:26

by Pekka Enberg

[permalink] [raw]
Subject: Re: 2.6.19: slight performance optimization for lib/string.c's strstrip()

On 12/8/06, Ulrich Windl <[email protected]> wrote:
> my apologies for disobeying all the rules for submitting patches, but I'll suggest
> a performance optimization for strstrip() in lib/string.c:

Makes sense. Please submit a patch.

2006-12-11 00:02:56

by Amit Choudhary

[permalink] [raw]
Subject: Re: 2.6.19: slight performance optimization for lib/string.c's strstrip()

> Suggested replacement:
>
> char *strstrip(char *s)
> {
> size_t size;
> char *end;
>
> while (*s && isspace(*s))
> s++;
> if (!*s)
> return s;
> size = strlen(s);
>
> end = s + size - 1;
> while (end > s && isspace(*end))
> end--;
> *(end + 1) = '\0';
>
> return s;
> }
> EXPORT_SYMBOL(strstrip);

How about this:

char *strstrip(char *s)
{
size_t less = 0;
char c = 0;
char *e = NULL;

while ((c=*s) && isspace(c))
s++;
if (!c)
return s;

e = s;

while (c=*e) {
less = isspace(c) ? (less + 1) : 0;
e++;
}

*(e-less) = 0;

return s;
}

1. no need to scan trailing spaces twice (once in strlen and then again).
2. pointer dereference only once per loop rather than multiple times.

Regards,
Amit

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

2006-12-11 03:37:25

by Amit Choudhary

[permalink] [raw]
Subject: Re: 2.6.19: slight performance optimization for lib/string.c's strstrip()


> How about this:
>
> char *strstrip(char *s)
> {
> size_t less = 0;
> char c = 0;
> char *e = NULL;
>
> while ((c=*s) && isspace(c))
> s++;
> if (!c)
> return s;
>
> e = s;
>
> while (c=*e) {
> less = isspace(c) ? (less + 1) : 0;
> e++;
> }
>
> *(e-less) = 0;
>
> return s;
> }
>

Well, this is not very efficient because it ends up calling isspace() function for all characters.
So, Ulrich's algo is the fastest.

-Amit



____________________________________________________________________________________
Yahoo! Music Unlimited
Access over 1 million songs.
http://music.yahoo.com/unlimited