Improving headersEnd call

From: Francisco Gimeno <kikov@dont-contact.us>
Date: Sun, 25 Feb 2007 18:18:06 +0100

Hello again,

as I have seen with oprofile (thanks to Adrian), the most time-consuming
function is headersEnd. I did a little debug on that, and I have found that
it's called a lot of times.

I have thought a improvement. I have seen that this functions is called
several-times to parse the same header.

As far I know, that function just take the number of bytes till we find two
\n\n (or \r\n \r\n). The problem is that it reparses everything for each TCP
packet (or whatever). So, we can add a new state var, called for example:
bytesParsed (a new field in conn->in), initialized to zero, and calling
headersEnd this way:

-------- original -------------
    if (t > conn->in.buf && t < (conn->in.buf + conn->in.offset - 8) &&
strncasecmp(t + 1, "HTTP/", 5) == 0) {
        if ((req_sz = headersEnd(conn->in.buf, conn->in.offset)) == 0) {
            debug(33, 5) ("Incomplete request, waiting for end of headers\n");
            *status = 0;
            return NULL;
        }
--------------------

------- proposed -----------
    if (t > conn->in.buf && t < (conn->in.buf + conn->in.offset - 8) &&
strncasecmp(t + 1, "HTTP/", 5) == 0) {
     if ((conn->in.bytesparsed = headersEnd(conn->in.buf, conn->in.offset)) ==
0) {
            debug(33, 5) ("Incomplete request, waiting for end of headers\n");
            *status = 0;
            conn->in.bytesparsed += conn->in.offset;
            return NULL;
     } else {
             req_sz = conn->in.bytesparsed;
     }
------------------------

I'm assuming that conn->in.offset contains the total length of the received
buffer. And conn->in.bytesparsed is initialized to 0 whenever is created.

For each use of headersEnd we could do this optimization.

Comments?

BR,
Francisco Gimeno

Received on Sun Feb 25 2007 - 10:12:22 MST

This archive was generated by hypermail pre-2.1.9 : Thu Mar 01 2007 - 12:00:02 MST