Skip to content

Commit

Permalink
remove uncommon branch from asciiprot hot path
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dormando committed Nov 10, 2011
1 parent 4538973 commit 67b6bb1
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}

/*
Expand Down

0 comments on commit 67b6bb1

Please sign in to comment.