This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Optimize strchr (x, 0)


I also noticed that strchr (x, 0) finds trailing zero by
rawmemchr(x, 0)

As strlen is special case of strchr I added macro that I call stplen
(Not quite sure with name, perhaps strend?) which returns pointer
instead size.

As strlen is more optimized than rawmemchr it will be more effective
than now. (If rawmemchr(x,0) is faster then it is performance regression of
strlen.)


git grep "strchr.*\\0" and git grep "rawmemchr.*\\0" return about 101
entries that could be simplified using stplen but it is not necessary
for performance.


2013-02-24  OndÅej BÃlka  <neleai@seznam.cz>

	* string/bits/string2.h: Add stplen macro.
	(rawmemchr) Optimize case rawmemchr(x, 0).
	(strchr) Use stplen.
	(strcat) Likewise.

---
 string/bits/string2.h |   18 ++++++++++++++++--
 1 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/string/bits/string2.h b/string/bits/string2.h
index f452bce..d40f2c7 100644
--- a/string/bits/string2.h
+++ b/string/bits/string2.h
@@ -28,6 +28,11 @@
 # define __BUILTIN_SINCE_GCC_3_2(fn) fn
 #endif
 
+#define __stplen(s) (__extension__ ({ char *__s = (char *) s;		      \
+				      __s + strlen (__s); }))
+
+
+
 #ifndef __NO_STRING_INLINES
 
 /* Unlike the definitions in the header <bits/string.h> the
@@ -401,10 +406,19 @@ __mempcpy_small (void *__dest, char __src1,
 extern void *__rawmemchr (const void *__s, int __c);
 # define strchr(s, c) \
   (__extension__ (__builtin_constant_p (c) && (c) == '\0'		      \
-		  ? (char *) __rawmemchr (s, c)				      \
+		  ? __stplen (s)					      \
 		  : __BUILTIN_SINCE_GCC_3_2 (strchr) (s, c)))
 #endif
 
+#ifndef _HAVE_STRING_ARCH_rawmemchr
+# define __rawmemchr(s, c) \
+  (__extension__ (__builtin_constant_p (c) && (c) == '\0'		      \
+		  ? __stplen (s)					      \
+		  : __rawmemchr (s, c)))
+#endif
+
+
+
 
 /* Copy SRC to DEST.  */
 #if (!defined _HAVE_STRING_ARCH_strcpy && !__GNUC_PREREQ (3, 0)) \
@@ -777,7 +791,7 @@ __stpcpy_small (char *__dest,
 		    __builtin_constant_p (src) && __builtin_constant_p (n)    \
 		    ? (strlen (src) < ((size_t) (n))			      \
 		       ? strcat (__dest, src)				      \
-		       : (*((char *) __mempcpy (strchr (__dest, '\0'),	      \
+		       : (*((char *) __mempcpy (stplen (__dest),	      \
 						src, n)) = '\0', __dest))     \
 		    : strncat (dest, src, n); }))
 # elif __GNUC_PREREQ (3, 2)
-- 
1.7.4.4


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]