lkml.org 
[lkml]   [2006]   [Dec]   [8]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
Date
Subject2.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

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

\
 
 \ /
  Last update: 2006-12-08 14:29    [W:0.028 / U:0.068 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site