From d58ab810a6e325cc351684d174c48cabce01bcc1 Mon Sep 17 00:00:00 2001 From: Wilco Dijkstra Date: Wed, 14 Dec 2016 15:12:18 +0000 Subject: Improve strtok and strtok_r performance. Instead of calling strpbrk which calls strcspn, call strcspn directly so we get the end of the token without an extra call to rawmemchr. Also avoid an unnecessary call to strcspn after the last token by adding an early exit for an empty string. Change strtok to tailcall strtok_r to avoid unnecessary code duplication. Remove the special header optimization for strtok_r of a 1-character constant string - both strspn and strcspn contain optimizations for this case. Benchmarking this showed similar performance in the worst case, but up to 5.5x better performance in the "found" case for large inputs. * benchtests/bench-strtok.c (oldstrtok): Add old implementation. * string/strtok.c (strtok): Change to tailcall __strtok_r. * string/strtok_r.c (__strtok_r): Optimize for performance. * string/string-inlines.c (__old_strtok_r_1c): New function. * string/bits/string2.h (__strtok_r): Move to string-inlines.c. --- string/bits/string2.h | 39 --------------------------------------- 1 file changed, 39 deletions(-) (limited to 'string/bits/string2.h') diff --git a/string/bits/string2.h b/string/bits/string2.h index ca1eda9bd1..e39d4f1a85 100644 --- a/string/bits/string2.h +++ b/string/bits/string2.h @@ -180,45 +180,6 @@ extern void *__rawmemchr (const void *__s, int __c); #endif -#if !defined _HAVE_STRING_ARCH_strtok_r || defined _FORCE_INLINES -# ifndef _HAVE_STRING_ARCH_strtok_r -# define __strtok_r(s, sep, nextp) \ - (__extension__ (__builtin_constant_p (sep) && __string2_1bptr_p (sep) \ - && ((const char *) (sep))[0] != '\0' \ - && ((const char *) (sep))[1] == '\0' \ - ? __strtok_r_1c (s, ((const char *) (sep))[0], nextp) \ - : __strtok_r (s, sep, nextp))) -# endif - -__STRING_INLINE char *__strtok_r_1c (char *__s, char __sep, char **__nextp); -__STRING_INLINE char * -__strtok_r_1c (char *__s, char __sep, char **__nextp) -{ - char *__result; - if (__s == NULL) - __s = *__nextp; - while (*__s == __sep) - ++__s; - __result = NULL; - if (*__s != '\0') - { - __result = __s++; - while (*__s != '\0') - if (*__s++ == __sep) - { - __s[-1] = '\0'; - break; - } - } - *__nextp = __s; - return __result; -} -# ifdef __USE_POSIX -# define strtok_r(s, sep, nextp) __strtok_r (s, sep, nextp) -# endif -#endif - - #if !defined _HAVE_STRING_ARCH_strsep || defined _FORCE_INLINES # ifndef _HAVE_STRING_ARCH_strsep -- cgit v1.2.3-70-g09d2