This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] Use strlen when searching for a nul char
- From: Wilco Dijkstra <Wilco dot Dijkstra at arm dot com>
- To: 'GNU C Library' <libc-alpha at sourceware dot org>
- Cc: nd <nd at arm dot com>
- Date: Fri, 15 Apr 2016 12:36:36 +0000
- Subject: Re: [PATCH] Use strlen when searching for a nul char
- Authentication-results: sourceware.org; auth=none
- Nodisclaimer: True
- References: <DB3PR08MB008978DA72CE0E1B93BB477483A60 at DB3PR08MB0089 dot eurprd08 dot prod dot outlook dot com>
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:23
ping
________________________________________
From: Wilco Dijkstra
Sent: 25 February 2016 13:04
To: 'GNU C Library'
Cc: nd
Subject: [PATCH] Use strlen when searching for a nul char
Remove the strchr (s, '\0') to rawmemchr optimization as using rawmemchr is
a bad idea - I have a patch to add strchr (s, '\0') -> strlen to GCC7.
Like strchr (s, '\0'), rawmemchr (s, '\0') appears a common idiom for finding
the end of a string, however it is not the most efficient way of doing so.
Strlen is a simpler operation which is significantly faster on larger inputs
(eg. on x86 strlen is 50% faster than rawmemchr on strings of 1KB).
ChangeLog:
2016-02-25 Wilco Dijkstra <wdijkstr@arm.com>
* string/bits/string2.h (strchr): Remove define.
(__rawmemchr): Add new define.
(rawmemchr): Likewise.
--
diff --git a/string/bits/string2.h b/string/bits/string2.h
index bebd158c5ff0f7bd7d9e4a4c3e120cd45b6e2143..f34fedb170352eaca0ed784ca6e76d7bbbfaefc2 100644
--- a/string/bits/string2.h
+++ b/string/bits/string2.h
@@ -62,14 +62,15 @@
#endif
-#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) \
- (__extension__ (__builtin_constant_p (c) && !__builtin_constant_p (s) \
- && (c) == '\0' \
- ? (char *) __rawmemchr (s, c) \
- : __builtin_strchr (s, c)))
+# define __rawmemchr(s, c) \
+ (__extension__ ({ char *__s = (char *)(s); \
+ __builtin_constant_p (c) && (c) == '\0' \
+ ? (void *)(__s + strlen (__s)) \
+ : __rawmemchr (__s, (c));}))
+# ifdef __USE_GNU
+# define rawmemchr(s,c) __rawmemchr ((s), (c))
# endif
#endif