]> git.saurik.com Git - redis.git/commitdiff
Re-use AOF buffer when it is small enough
authorPieter Noordhuis <pcnoordhuis@gmail.com>
Thu, 18 Aug 2011 10:44:30 +0000 (12:44 +0200)
committerantirez <antirez@gmail.com>
Tue, 13 Sep 2011 10:22:54 +0000 (12:22 +0200)
src/aof.c
src/sds.c
src/sds.h

index 0ebe4457d95a3e26d823ceb5a03010dc76945124..60694dbfa26634bc5184c623c58a33958b5fa4ce 100644 (file)
--- a/src/aof.c
+++ b/src/aof.c
@@ -81,10 +81,17 @@ void flushAppendOnlyFile(void) {
         }
         exit(1);
     }
-    sdsfree(server.aofbuf);
-    server.aofbuf = sdsempty();
     server.appendonly_current_size += nwritten;
 
+    /* Re-use AOF buffer when it is small enough. The maximum comes from the
+     * arena size of 4k minus some overhead (but is otherwise arbitrary). */
+    if ((sdslen(server.aofbuf)+sdsavail(server.aofbuf)) < 4000) {
+        sdsclear(server.aofbuf);
+    } else {
+        sdsfree(server.aofbuf);
+        server.aofbuf = sdsempty();
+    }
+
     /* Don't fsync if no-appendfsync-on-rewrite is set to yes and there are
      * children doing I/O in the background. */
     if (server.no_appendfsync_on_rewrite &&
index 2ec7c3cb76b3a2d6f5afdb3be8bd6aa793329a1c..77052966ceb79e72355df042f400648d35131829 100644 (file)
--- a/src/sds.c
+++ b/src/sds.c
@@ -94,6 +94,13 @@ void sdsupdatelen(sds s) {
     sh->len = reallen;
 }
 
+void sdsclear(sds s) {
+    struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
+    sh->free += sh->len;
+    sh->len = 0;
+    sh->buf[0] = '\0';
+}
+
 static sds sdsMakeRoomFor(sds s, size_t addlen) {
     struct sdshdr *sh, *newsh;
     size_t free = sdsavail(s);
index af5c4910bdae766730c776691fdd2019d18d920c..6e5684eeb913e370f2c0907ac15262622d19873e 100644 (file)
--- a/src/sds.h
+++ b/src/sds.h
@@ -76,6 +76,7 @@ sds sdscatprintf(sds s, const char *fmt, ...);
 sds sdstrim(sds s, const char *cset);
 sds sdsrange(sds s, int start, int end);
 void sdsupdatelen(sds s);
+void sdsclear(sds s);
 int sdscmp(sds s1, sds s2);
 sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count);
 void sdsfreesplitres(sds *tokens, int count);