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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[Index of Archives]     [Kernel Newbies]     [Netfilter]     [Bugtraq]     [Photo]     [Stuff]     [Gimp]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Video 4 Linux]     [Linux for the blind]     [Linux Resources]
  Powered by Linux