[PATCH] Improve performance of strncpy
Wilco Dijkstra
wdijkstr@arm.com
Wed Sep 10 15:21:00 GMT 2014
Adhemerval Zanella wrote:
> Hi, the patch looks ok. I also pushed a similar modification for powerpc based on same idea.
> zero_fill:
> - do
> - *++s1 = '\0';
> - while (--n > 0);
> + if (n >= 8)
> + memset (s1 + 1, '\0', n);
> + else
> + do
> + *++s1 = '\0';
> + while (--n > 0);
> I wonder if this test is really worth, my opinion is just to keep it simple
> and just call memset on both 'goto' in loop and after 'last_chars'.
Yes, you're right, I timed it and there is actually little difference, while
the code is now even simpler. New version below (not attaching results in bad
characters due to various mail servers changing line endings).
OK for commit?
---
string/strncpy.c | 14 +++++---------
1 file changed, 5 insertions(+), 9 deletions(-)
diff --git a/string/strncpy.c b/string/strncpy.c
index 0915e03..d5fa5be 100644
--- a/string/strncpy.c
+++ b/string/strncpy.c
@@ -57,10 +57,10 @@ STRNCPY (char *s1, const char *s2, size_t n)
if (--n4 == 0)
goto last_chars;
}
- n = n - (s1 - s) - 1;
- if (n == 0)
- return s;
- goto zero_fill;
+ s1++;
+ n = n - (s1 - s);
+ memset (s1, '\0', n);
+ return s;
}
last_chars:
@@ -77,11 +77,7 @@ STRNCPY (char *s1, const char *s2, size_t n)
}
while (c != '\0');
- zero_fill:
- do
- *++s1 = '\0';
- while (--n > 0);
-
+ memset (s1 + 1, '\0', n);
return s;
}
libc_hidden_builtin_def (strncpy)
--
1.7.9.5
More information about the Libc-alpha
mailing list