From 67b6bb1d04994e638ccdae6b738e6b960af0858d Mon Sep 17 00:00:00 2001 From: dormando Date: Fri, 30 Sep 2011 01:22:16 -0700 Subject: [PATCH] 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. --- memcached.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/memcached.c b/memcached.c index f2d539cfae..0c1db666cc 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++; } /*