Messages in this thread Patch in this message |  | | | Date | Sun, 10 Dec 2006 21:52:30 +0100 | | From | Folkert van Heusden <> | | Subject | strncpy optimalisation? (lib/string.c) |
| |
Hi,
In lib/string.c we have:
char *strncpy(char *dest, const char *src, size_t count) { char *tmp = dest; while (count) { if ((*tmp = *src) != 0) src++; tmp++; count--; } return dest; } now I wonder isn't this ineffecient when strlen(src) < count? It would then, if I'm correct, iterate count-strlen(src) times doing useless increment/decrement. And since there are aprox. 580 instances in the 2.6.18.2 source, maybe some efficency can be won here. Wouldn't it be better to do: if ((*tmp = *src) == 0x00) break; So that would be: --- lib/string.c 2006-11-04 02:33:58.000000000 +0100 +++ string-new.c 2006-12-10 21:50:05.000000000 +0100 @@ -97,8 +97,8 @@ char *tmp = dest; while (count) { - if ((*tmp = *src) != 0) - src++; + if ((*tmp = *src) == 0x00) + break; tmp++; count--; }
Folkert van Heusden
-- www.vanheusden.com/multitail - win een vlaai van multivlaai! zorg ervoor dat multitail opgenomen wordt in Fedora Core, AIX, Solaris of HP/UX en win een vlaai naar keuze ---------------------------------------------------------------------- Phone: +31-6-41278122, PGP-key: 1F28D8AE, www.vanheusden.com - 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/
|  |