Skip to content

Commit

Permalink
optimize eol search for header parsing etc. (27% gain on core2)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuho committed May 11, 2010
1 parent d20daab commit b43f8ba
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions picohttpparser.c
@@ -1,8 +1,16 @@
#include <stddef.h>
#include "picohttpparser.h"

#ifdef __GNUC__
# define likely(x) __builtin_expect(!!(x), 1)
# define unlikely(x) __builtin_expect(!!(x), 0)
#else
# define likely(x) (x)
# define unlikely(x) (x)
#endif

#define CHECK_EOF() \
if (buf == buf_end) { \
if (buf == buf_end) { \
*ret = -2; \
return NULL; \
}
Expand All @@ -29,23 +37,24 @@
toklen = buf - tok_start; \
} while (0)

#define ADVANCE_EOL(tok, toklen) do { \
const char* tok_start = buf; \
for (; ; ++buf) { \
CHECK_EOF(); \
if (*buf == '\r' || *buf == '\n') { \
break; \
} \
} \
if (*buf == '\r') { \
++buf; \
EXPECT_CHAR('\n'); \
toklen = buf - 2 - tok_start; \
} else { /* should be: *buf == '\n' */ \
toklen = buf - tok_start; \
++buf; \
} \
tok = tok_start; \
#define ADVANCE_EOL(tok, toklen) do { \
const char* tok_start = buf; \
for (; ; ++buf) { \
CHECK_EOF(); \
if (unlikely((unsigned char)*buf <= '\r') && \
(*buf == '\r' || *buf == '\n')) { \
break; \
} \
} \
if (*buf == '\r') { \
++buf; \
EXPECT_CHAR('\n'); \
toklen = buf - 2 - tok_start; \
} else { /* should be: *buf == '\n' */ \
toklen = buf - tok_start; \
++buf; \
} \
tok = tok_start; \
} while (0)

static const char* is_complete(const char* buf, const char* buf_end,
Expand Down

0 comments on commit b43f8ba

Please sign in to comment.