This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH 4/* v2] Optimize strchrnul more
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: libc-alpha at sourceware dot org
- Cc: Wilco Dijkstra <wdijkstr at arm dot com>
- Date: Sun, 24 May 2015 19:10:36 +0200
- Subject: Re: [PATCH 4/* v2] Optimize strchrnul more
- Authentication-results: sourceware.org; auth=none
- References: <20150524150715 dot GA31589 at domone> <20150524163214 dot GA28053 at domone>
On Sun, May 24, 2015 at 06:32:14PM +0200, OndÅej BÃlka wrote:
> Hi,
> this is nontrivial optimization of string inlines.
> First it decreases icache pressure as you don't need strchr.
>
Just realized that optimization there is silly way to find terminating
zero. On x64 rawmemchr is around 50% slower than strlen so add rawmemchr
special case that does just that.
* string/bits/string2.h (strchrnul, rawmemchr): Add inline
(strchr): Optimize.
diff --git a/string/bits/string2.h b/string/bits/string2.h
index 2fe67b3..8f1eb04 100644
--- a/string/bits/string2.h
+++ b/string/bits/string2.h
@@ -108,18 +108,39 @@ __STRING2_COPY_TYPE (8);
#endif
-/* Return pointer to C in S. */
-#ifndef _HAVE_STRING_ARCH_strchr
+#ifndef _HAVE_STRING_ARCH_rawmemchr
extern void *__rawmemchr (const void *__s, int __c);
# if __GNUC_PREREQ (3, 2)
-# define strchr(s, c) \
+# define __rawmemchr(s, c) \
+ (__extension__ (__builtin_constant_p (c) && !__builtin_constant_p (s) \
+ && (c) == '\0' \
+ ? s + strlen (s) \
+ : __rawmemchr (s, c)))
+# endif
+#endif
+
+
+
+#ifndef _HAVE_STRING_ARCH_strchrnul
+# if __GNUC_PREREQ (3, 2)
+# define strchrnul(s, c) \
(__extension__ (__builtin_constant_p (c) && !__builtin_constant_p (s) \
&& (c) == '\0' \
? (char *) __rawmemchr (s, c) \
- : __builtin_strchr (s, c)))
+ : strchrnul (s, c)))
# endif
#endif
+/* Return pointer to C in S. */
+#ifndef _HAVE_STRING_ARCH_strchr
+# if __GNUC_PREREQ (3, 2)
+# define strchr(s, c) \
+ (__extension__ ({ char *__r = strchrnul (s, c); \
+ *__r == c ? __r : NULL; }))
+# endif
+#endif
+
+
/* Copy SRC to DEST, returning pointer to final NUL byte. */
#ifdef __USE_GNU
# if !defined _HAVE_STRING_ARCH_stpcpy || defined _FORCE_INLINES