From 34a719d25034d6f1140a10eb0429bdee0efa5cd9 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 17 Sep 2010 15:25:32 +0200 Subject: [PATCH] try to parse the request in a smarter way to gain speed... work in progress --- src/networking.c | 13 +++++++++++-- src/redis.h | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/networking.c b/src/networking.c index 867032aa..aaf7518f 100644 --- a/src/networking.c +++ b/src/networking.c @@ -28,6 +28,7 @@ redisClient *createClient(int fd) { selectDb(c,0); c->fd = fd; c->querybuf = sdsempty(); + c->newline = NULL; c->argc = 0; c->argv = NULL; c->bulklen = -1; @@ -631,6 +632,7 @@ void resetClient(redisClient *c) { freeClientArgv(c); c->bulklen = -1; c->multibulk = 0; + c->newline = NULL; } void closeTimedoutClients(void) { @@ -672,13 +674,14 @@ again: if (c->flags & REDIS_BLOCKED || c->flags & REDIS_IO_WAIT) return; if (c->bulklen == -1) { /* Read the first line of the query */ - char *p = strchr(c->querybuf,'\n'); size_t querylen; - if (p) { + if (c->newline) { + char *p = c->newline; sds query, *argv; int argc, j; + c->newline = NULL; query = c->querybuf; c->querybuf = sdsempty(); querylen = 1+(p-(query)); @@ -765,8 +768,14 @@ void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) { return; } if (nread) { + size_t oldlen = sdslen(c->querybuf); c->querybuf = sdscatlen(c->querybuf, buf, nread); c->lastinteraction = time(NULL); + /* Scan this new piece of the query for the newline. We do this + * here in order to make sure we perform this scan just one time + * per piece of buffer, leading to an O(N) scan instead of O(N*N) */ + if (c->bulklen == -1 && c->newline == NULL) + c->newline = strchr(c->querybuf+oldlen,'\n'); } else { return; } diff --git a/src/redis.h b/src/redis.h index 38727ae2..e6166f8b 100644 --- a/src/redis.h +++ b/src/redis.h @@ -286,6 +286,7 @@ typedef struct redisClient { int dictid; sds querybuf; robj **argv, **mbargv; + char *newline; /* pointing to the detected newline in querybuf */ int argc, mbargc; long bulklen; /* bulk read len. -1 if not in bulk read mode */ int multibulk; /* multi bulk command format active */ -- GitLab