2.6.19: slight performance optimization for lib/string.c's strstrip()

From: Ulrich Windl
Date: Fri Dec 08 2006 - 08:27:08 EST


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

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/