Right now spamd reallocs in 8K increments when reading config data
from spamd-setup.  This makes it very slow to load large blacklists.
There is also an extra realloc() that gets used to add space for
the terminating NUL if needed.

This diff changes it to realloc in 1M increments and free the config
buffer between spamd-setup invocations (since it can grow to be
quite large).

This speeds up config reading substantially with the large blacklists
I use (biggest is 360M) and freeing the buffer reduces the stable
process size by about 400M in my case.

 - todd

--- spamd.c.poll        Mon Dec 29 16:10:44 2014
+++ spamd.c     Tue Dec 30 16:01:35 2014
@@ -268,23 +268,9 @@
 parse_configs(void)
 {
        char *start, *end;
-       int i;
+       size_t i;
 
-       if (cbu == cbs) {
-               char *tmp;
-
-               tmp = realloc(cb, cbs + 8192);
-               if (tmp == NULL) {
-                       if (debug > 0)
-                               warn("realloc");
-                       free(cb);
-                       cb = NULL;
-                       cbs = cbu = 0;
-                       return;
-               }
-               cbs += 8192;
-               cb = tmp;
-       }
+       /* We always leave an extra byte for the NUL. */
        cb[cbu++] = '\0';
 
        start = cb;
@@ -310,19 +296,17 @@
        if (debug > 0)
                printf("got configuration connection\n");
 
-       if (cbu == cbs) {
+       /* Leave an extra byte for the terminating NUL. */
+       if (cbu + 1 >= cbs) {
                char *tmp;
 
-               tmp = realloc(cb, cbs + 8192);
+               tmp = realloc(cb, cbs + (1024 * 1024));
                if (tmp == NULL) {
                        if (debug > 0)
                                warn("realloc");
-                       free(cb);
-                       cb = NULL;
-                       cbs = 0;
                        goto configdone;
                }
-               cbs += 8192;
+               cbs += 1024 * 1024;
                cb = tmp;
        }
 
@@ -330,7 +314,8 @@
        if (debug > 0)
                printf("read %d config bytes\n", n);
        if (n == 0) {
-               parse_configs();
+               if (cbu != 0)
+                       parse_configs();
                goto configdone;
        } else if (n == -1) {
                if (debug > 0)
@@ -341,6 +326,9 @@
        return;
 
 configdone:
+       free(cb);
+       cb = NULL;
+       cbs = 0;
        cbu = 0;
        close(conffd);
        conffd = -1;

Reply via email to