Hi list,

I'm currently trying to reduce httpd memory footprint. (http://www.mail-archive.com/dev@httpd.apache.org/msg56558.html)

On my test machine, processing a request requires about 15ko in the 'request' pool. I'm trying to reduce it to only use 8k which is the minimum allocated on Linux (min: 2 x 4ko pages)


As a first step, I noticed that apr_itoa, apr_ltoa, apr_off_t_toa could be tweaked to require less memory for some common cases. The attached patch reduces memory use for small values, that is to say for strings that fit in 8 bytes (including NULL)


On my system:
apr_itoa allocates 14 (4*3+2) bytes, which gets rounded to 16. A common use is for HTTP status code which fits in much less.
--> 8 bytes can be saved in the request pool

apr_off_t_toa allocates 26 (8*3+2) bytes, which gets rounded to 32. A common use is for the length of the answer. Common pages are well under 9.999.999 bytes.
--> 24 bytes can be saved in the request pool


This is not that much, but is a start.

Best regards,
CJ
Index: strings/apr_strings.c
===================================================================
--- strings/apr_strings.c       (révision 1460121)
+++ strings/apr_strings.c       (copie de travail)
@@ -359,10 +359,17 @@
 
 APR_DECLARE(char *) apr_itoa(apr_pool_t *p, int n)
 {
-    const int BUFFER_SIZE = sizeof(int) * 3 + 2;
-    char *buf = apr_palloc(p, BUFFER_SIZE);
-    char *start = buf + BUFFER_SIZE - 1;
+    int BUFFER_SIZE = sizeof(int) * 3 + 2;
+    char *buf, *start;
     int negative;
+
+    /* apr_palloc aligns memory on 8 bytes (see apr_general.h).
+     * So, for small values of 'n', do not over-allocate some memory. */
+    if (sizeof(int) >= 4 && n<=9999999L && n>=-999999L)
+        BUFFER_SIZE = 8;
+
+    buf = apr_palloc(p, BUFFER_SIZE);
+    start = buf + BUFFER_SIZE - 1;
     if (n < 0) {
        negative = 1;
        n = -n;
@@ -383,10 +390,17 @@
 
 APR_DECLARE(char *) apr_ltoa(apr_pool_t *p, long n)
 {
-    const int BUFFER_SIZE = sizeof(long) * 3 + 2;
-    char *buf = apr_palloc(p, BUFFER_SIZE);
-    char *start = buf + BUFFER_SIZE - 1;
+    int BUFFER_SIZE = sizeof(long) * 3 + 2;
+    char *buf, *start;
     int negative;
+
+    /* apr_palloc aligns memory on 8 bytes (see apr_general.h).
+     * So, for small values of 'n', do not over-allocate some memory. */
+    if (sizeof(long) >= 4 && n<=9999999L && n>=-999999L)
+        BUFFER_SIZE = 8;
+
+    buf = apr_palloc(p, BUFFER_SIZE);
+    start = buf + BUFFER_SIZE - 1;
     if (n < 0) {
        negative = 1;
        n = -n;
@@ -407,10 +421,17 @@
 
 APR_DECLARE(char *) apr_off_t_toa(apr_pool_t *p, apr_off_t n)
 {
-    const int BUFFER_SIZE = sizeof(apr_off_t) * 3 + 2;
-    char *buf = apr_palloc(p, BUFFER_SIZE);
-    char *start = buf + BUFFER_SIZE - 1;
+    int BUFFER_SIZE = sizeof(apr_off_t) * 3 + 2;
+    char *buf, *start;
     int negative;
+
+    /* apr_palloc aligns memory on 8 bytes (see apr_general.h).
+     * So, for small values of 'n', do not over-allocate some memory. */
+    if (sizeof(apr_off_t) >= 4 && n<=9999999L && n>=-999999L)
+        BUFFER_SIZE = 8;
+
+    buf = apr_palloc(p, BUFFER_SIZE);
+    start = buf + BUFFER_SIZE - 1;
     if (n < 0) {
        negative = 1;
        n = -n;

Reply via email to