# HG changeset patch # User Igor Sysoev # Date 1181025848 0 # Node ID f8a40ccafb041d9604828ec8daeb65ad6da3ac35 # Parent 6be3d90732668ee3538870692f7b014013f6176d optimize regex location search diff -r 6be3d9073266 -r f8a40ccafb04 src/http/ngx_http_core_module.c --- a/src/http/ngx_http_core_module.c Mon Jun 04 20:45:45 2007 +0000 +++ b/src/http/ngx_http_core_module.c Tue Jun 05 06:44:08 2007 +0000 @@ -29,7 +29,7 @@ static ngx_int_t ngx_http_core_find_location(ngx_http_request_t *r, - ngx_array_t *locations, size_t len); + ngx_array_t *locations, ngx_uint_t regex_start, size_t len); static ngx_int_t ngx_http_core_preconfiguration(ngx_conf_t *cf); static void *ngx_http_core_create_main_conf(ngx_conf_t *cf); @@ -629,7 +629,7 @@ cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); - rc = ngx_http_core_find_location(r, &cscf->locations, 0); + rc = ngx_http_core_find_location(r, &cscf->locations, cscf->regex_start, 0); if (rc == NGX_HTTP_INTERNAL_SERVER_ERROR) { ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); @@ -930,7 +930,7 @@ static ngx_int_t ngx_http_core_find_location(ngx_http_request_t *r, - ngx_array_t *locations, size_t len) + ngx_array_t *locations, ngx_uint_t regex_start, size_t len) { ngx_int_t n, rc; ngx_uint_t i, found, noregex; @@ -1009,7 +1009,8 @@ clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); if (clcf->locations.nelts) { - rc = ngx_http_core_find_location(r, &clcf->locations, len); + rc = ngx_http_core_find_location(r, &clcf->locations, + clcf->regex_start, len); if (rc != NGX_OK) { return rc; @@ -1025,11 +1026,7 @@ /* regex matches */ - for (/* void */; i < locations->nelts; i++) { - - if (!clcfp[i]->regex) { - continue; - } + for (i = regex_start; i < locations->nelts; i++) { if (clcfp[i]->noname) { break; @@ -1553,14 +1550,15 @@ static char * ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) { - char *rv; - void *mconf; - ngx_uint_t m; - ngx_conf_t pcf; - ngx_http_module_t *module; - ngx_http_conf_ctx_t *ctx, *http_ctx; - ngx_http_core_srv_conf_t *cscf, **cscfp; - ngx_http_core_main_conf_t *cmcf; + char *rv; + void *mconf; + ngx_uint_t i, m; + ngx_conf_t pcf; + ngx_http_module_t *module; + ngx_http_conf_ctx_t *ctx, *http_ctx; + ngx_http_core_srv_conf_t *cscf, **cscfp; + ngx_http_core_loc_conf_t **clcfp; + ngx_http_core_main_conf_t *cmcf; ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)); if (ctx == NULL) { @@ -1644,6 +1642,16 @@ ngx_sort(cscf->locations.elts, (size_t) cscf->locations.nelts, sizeof(ngx_http_core_loc_conf_t *), ngx_http_core_cmp_locations); + cscf->regex_start = cscf->locations.nelts; + clcfp = cscf->locations.elts; + + for (i = 0; i < cscf->locations.nelts; i++) { + if (clcfp[i]->regex) { + cscf->regex_start = i; + break; + } + } + return rv; } @@ -1652,7 +1660,7 @@ ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) { char *rv; - ngx_int_t m; + ngx_uint_t i, m; ngx_str_t *value; ngx_conf_t save; ngx_http_module_t *module; @@ -1816,6 +1824,16 @@ ngx_sort(clcf->locations.elts, (size_t) clcf->locations.nelts, sizeof(ngx_http_core_loc_conf_t *), ngx_http_core_cmp_locations); + clcf->regex_start = clcf->locations.nelts; + clcfp = clcf->locations.elts; + + for (i = 0; i < clcf->locations.nelts; i++) { + if (clcfp[i]->regex) { + clcf->regex_start = i; + break; + } + } + return rv; } diff -r 6be3d9073266 -r f8a40ccafb04 src/http/ngx_http_core_module.h --- a/src/http/ngx_http_core_module.h Mon Jun 04 20:45:45 2007 +0000 +++ b/src/http/ngx_http_core_module.h Tue Jun 05 06:44:08 2007 +0000 @@ -117,6 +117,9 @@ */ ngx_array_t locations; + unsigned regex_start:16; + unsigned wildcard:1; + /* array of the ngx_http_listen_t, "listen" directive */ ngx_array_t listen; @@ -138,8 +141,6 @@ ngx_flag_t optimize_server_names; ngx_flag_t ignore_invalid_headers; - - ngx_uint_t wildcard; /* unsigned wildcard:1 */ } ngx_http_core_srv_conf_t; @@ -210,6 +211,8 @@ ngx_regex_t *regex; #endif + unsigned regex_start:16; + unsigned noname:1; /* "if () {}" block */ unsigned exact_match:1;