This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH] Improve memccpy performance
- From: "Wilco Dijkstra" <wdijkstr at arm dot com>
- To: <libc-alpha at sourceware dot org>
- Date: Wed, 14 Jan 2015 14:17:49 -0000
- Subject: [PATCH] Improve memccpy performance
- Authentication-results: sourceware.org; auth=none
Improve memccpy performance by using memchr/memcpy rather than a byte loop.
Overall performance on bench-memccpy is > 2x faster when using the C
implementation of memchr and an optimized memcpy.
ChangeLog:
2015-01-14 Wilco Dijkstra wdijkstr@arm.com
* string/memccpy.c (memccpy): Improve performance using memchr/memcpy.
---
string/memccpy.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/string/memccpy.c b/string/memccpy.c
index 70ee2ae..d4146f9 100644
--- a/string/memccpy.c
+++ b/string/memccpy.c
@@ -26,15 +26,15 @@
void *
__memccpy (void *dest, const void *src, int c, size_t n)
{
- const char *s = src;
- char *d = dest;
- const char x = c;
- size_t i = n;
+ void *p = memchr (src, c, n);
- while (i-- > 0)
- if ((*d++ = *s++) == x)
- return d;
+ if (p != NULL)
+ {
+ n = p - src + 1;
+ return memcpy (dest, src, n) + n;
+ }
+ memcpy (dest, src, n);
return NULL;
}
--
1.9.1