diff options
author | Wilco Dijkstra <wdijkstr@arm.com> | 2016-12-14 15:12:18 +0000 |
---|---|---|
committer | Wilco Dijkstra <wdijkstr@arm.com> | 2016-12-14 15:12:18 +0000 |
commit | d58ab810a6e325cc351684d174c48cabce01bcc1 (patch) | |
tree | 52a582dd886d5931988c3a6e2d1acd79efe2fe33 /benchtests | |
parent | 14348aaeff5ccb136e3fe967b86f97b9cea950a2 (diff) | |
download | glibc-d58ab810a6e325cc351684d174c48cabce01bcc1.tar.gz |
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.
Diffstat (limited to 'benchtests')
-rw-r--r-- | benchtests/bench-strtok.c | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/benchtests/bench-strtok.c b/benchtests/bench-strtok.c index eeb798f015..41e0e45db8 100644 --- a/benchtests/bench-strtok.c +++ b/benchtests/bench-strtok.c @@ -20,13 +20,41 @@ #define TEST_NAME "strtok" #include "bench-string.h" -#define STRTOK strtok_string -#include <string/strtok.c> +char * +oldstrtok (char *s, const char *delim) +{ + static char *olds; + char *token; + + if (s == NULL) + s = olds; + + /* Scan leading delimiters. */ + s += strspn (s, delim); + if (*s == '\0') + { + olds = s; + return NULL; + } + /* Find the end of the token. */ + token = s; + s = strpbrk (token, delim); + if (s == NULL) + /* This token finishes the string. */ + olds = __rawmemchr (token, '\0'); + else + { + /* Terminate the token and make OLDS point past it. */ + *s = '\0'; + olds = s + 1; + } + return token; +} typedef char *(*proto_t) (const char *, const char *); -IMPL (strtok_string, 0) +IMPL (oldstrtok, 0) IMPL (strtok, 1) static void |