diff options
author | dormando <dormando@rydia.net> | 2011-09-30 01:22:16 -0700 |
---|---|---|
committer | dormando <dormando@rydia.net> | 2011-11-09 16:06:08 -0800 |
commit | 67b6bb1d04994e638ccdae6b738e6b960af0858d (patch) | |
tree | a437091538776c4a68ccdb97407d0dbf76af8f1f | |
parent | 4538973592a4c59e520eedf9a4703a4c11c26d0a (diff) | |
download | memcached-67b6bb1d04994e638ccdae6b738e6b960af0858d.tar.gz |
remove uncommon branch from asciiprot hot path
The \0 test in the loop was accounting for 2% of memcached's CPU usage
according to callgrind. strlen is an SSE4 instruction and can sniff out that
null byte quickly.
-rw-r--r-- | memcached.c | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/memcached.c b/memcached.c index f2d539c..0c1db66 100644 --- a/memcached.c +++ b/memcached.c @@ -2390,28 +2390,34 @@ typedef struct token_s { static size_t tokenize_command(char *command, token_t *tokens, const size_t max_tokens) { char *s, *e; size_t ntokens = 0; + size_t len = strlen(command); + unsigned int i = 0; assert(command != NULL && tokens != NULL && max_tokens > 1); - for (s = e = command; ntokens < max_tokens - 1; ++e) { + s = e = command; + for (i = 0; i < len; i++) { if (*e == ' ') { if (s != e) { tokens[ntokens].value = s; tokens[ntokens].length = e - s; ntokens++; *e = '\0'; + if (ntokens == max_tokens - 1) { + e++; + s = e; /* so we don't add an extra token */ + break; + } } s = e + 1; } - else if (*e == '\0') { - if (s != e) { - tokens[ntokens].value = s; - tokens[ntokens].length = e - s; - ntokens++; - } + e++; + } - break; /* string end */ - } + if (s != e) { + tokens[ntokens].value = s; + tokens[ntokens].length = e - s; + ntokens++; } /* |