[PATCH] fast AND correct strncpy
[Posted August 20, 2003 by corbet]
| From: |
| Linux Kernel Mailing List <linux-kernel@vger.kernel.org> |
| To: |
| bk-commits-head@vger.kernel.org |
| Subject: |
| [PATCH] fast AND correct strncpy |
| Date: |
| Sun, 17 Aug 2003 15:48:39 +0000 |
ChangeSet 1.1207, 2003/08/17 08:48:39-07:00, albert@users.sourceforge.net
[PATCH] fast AND correct strncpy
This is Erik Andersen's excellent strncpy.
It works like magic. That "if" isn't a jump;
gcc uses a few integer instructions to wipe
out all jumps except for the loop itself and
the function call/return.
This has been exhaustively tested against glibc.
The existing code has 5 extra branches and
is over twice as large. (my gcc, etc.)
# This patch includes the following deltas:
# ChangeSet 1.1206 -> 1.1207
# lib/string.c 1.11 -> 1.12
#
string.c | 9 ++++-----
1 files changed, 4 insertions(+), 5 deletions(-)
diff -Nru a/lib/string.c b/lib/string.c
--- a/lib/string.c Sun Aug 17 09:06:29 2003
+++ b/lib/string.c Sun Aug 17 09:06:29 2003
@@ -87,13 +87,12 @@
{
char *tmp = dest;
- while (count && (*dest++ = *src++) != '\0')
- count--;
- while (count > 1) {
- *dest++ = 0;
+ while (count) {
+ if ((*tmp = *src) != 0) src++;
+ tmp++;
count--;
}
- return tmp;
+ return dest;
}
#endif
-
To unsubscribe from this list: send the line "unsubscribe bk-commits-head" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
(
Log in to post comments)