Adding tests for handlers and filters

Marcus Clyne maccaday at gmail.com
Fri Aug 14 02:05:21 MSD 2009


Hi,

Would it not be a good idea to add tests at conf-read time to see if 
headers/filters need to be added into the source code to avoid the 
unnecessary overhead of checking whether they need to be used or not at 
runtime?

For example, the SSI module.  The filter is automatically added to the 
list in:

static ngx_int_t
ngx_http_ssi_filter_init(ngx_conf_t *cf)
{
    ngx_http_next_header_filter = ngx_http_top_header_filter;
    ngx_http_top_header_filter = ngx_http_ssi_header_filter;

    ngx_http_next_body_filter = ngx_http_top_body_filter;
    ngx_http_top_body_filter = ngx_http_ssi_body_filter;

    return NGX_OK;
}

I believe a better implementation would include a variable (it might as 
well be global) to test whether the filter needs to be installed, e.g.

ngx_int_t   http_ssi_filter_use = 0;
...
[NEW FUNCTION - replacing the use of ngx_conf_set_flag_slot for 
directive 'ssi']
static char*
ngx_http_ssi (ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    // test for on/off (like ngx_conf_set_flag_slot)
    http_ssi_filter_use = 1;
    return   NGX_CONF_OK;
}

static ngx_int_t
ngx_http_ssi_filter_init (ngx_conf_t *cf)
{
    if (http_ssi_filter_use) {
        ngx_http_next_header_filter = ngx_http_top_header_filter;
        ngx_http_top_header_filter = ngx_http_ssi_header_filter;

        ngx_http_next_body_filter = ngx_http_top_body_filter;
        ngx_http_top_body_filter = ngx_http_ssi_body_filter;
    }

    return NGX_OK;
}


Doing this for all the modules, including headers and footers (if it's 
not already done) I think would make a difference in speed.  Some 
modules wouldn't need creating a new function, just adding the 
http_[module]_use = 1 to an existing conf function, and the tests for 
adding handlers / filters.

Most configurations probably do not use that many handlers / filters, 
and by eliminating all the additional function pointer calls, checks and 
returns could add a little performance increase to the server as a 
whole, and would not affect the functionality of the server in any way.

Cheers,

Marcus.





More information about the nginx mailing list