From 111f35f4e1962cbfda3adf7ab64927da6e9b0b2d Mon Sep 17 00:00:00 2001 From: Christophe Jaillet Date: Wed, 22 May 2013 20:38:35 +0000 Subject: [PATCH] Be more clever when allocating memory for log item to be escaped. This should be faster and save about 70-100 bytes in the request pool with the default config. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1485409 13f79535-47bb-0310-9956-ffa450edef68 --- server/util.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/server/util.c b/server/util.c index c696fb7350..7a9e47cf46 100644 --- a/server/util.c +++ b/server/util.c @@ -1910,16 +1910,33 @@ AP_DECLARE(char *) ap_escape_logitem(apr_pool_t *p, const char *str) char *ret; unsigned char *d; const unsigned char *s; + apr_size_t length, escapes = 0; if (!str) { return NULL; } - ret = apr_palloc(p, 4 * strlen(str) + 1); /* Be safe */ + /* Compute how many characters need to be escaped */ + s = (const unsigned char *)str; + for (; *s; ++s) { + if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) { + escapes++; + } + } + + /* Compute the length of the input string, including NULL */ + length = s - (const unsigned char *)str + 1; + + /* Fast path: nothing to escape */ + if (escapes == 0) { + return apr_pmemdup(p, str, length); + } + + /* Each escaped character needs up to 3 extra bytes (0 --> \x00) */ + ret = apr_palloc(p, length + 3 * escapes); d = (unsigned char *)ret; s = (const unsigned char *)str; for (; *s; ++s) { - if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) { *d++ = '\\'; switch(*s) { -- 2.40.0