changeset 1235:f8a40ccafb04

optimize regex location search
author Igor Sysoev <igor@sysoev.ru>
date Tue, 05 Jun 2007 06:44:08 +0000
parents 6be3d9073266
children 5e4d3b5d7ad0
files src/http/ngx_http_core_module.c src/http/ngx_http_core_module.h
diffstat 2 files changed, 41 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -29,7 +29,7 @@ typedef struct {
 
 
 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 @@ ngx_http_core_find_config_phase(ngx_http
 
     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 @@ ngx_http_update_location_config(ngx_http
 
 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 @@ ngx_http_core_find_location(ngx_http_req
         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 @@ ngx_http_core_find_location(ngx_http_req
 
     /* 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 @@ ngx_http_cleanup_add(ngx_http_request_t 
 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_http_core_server(ngx_conf_t *cf, ngx
     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 @@ static char *
 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_http_core_location(ngx_conf_t *cf, n
     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;
 }
 
--- a/src/http/ngx_http_core_module.h
+++ b/src/http/ngx_http_core_module.h
@@ -117,6 +117,9 @@ typedef struct {
      */
     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 @@ typedef struct {
 
     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 @@ struct ngx_http_core_loc_conf_s {
     ngx_regex_t  *regex;
 #endif
 
+    unsigned      regex_start:16;
+
     unsigned      noname:1;   /* "if () {}" block */
 
     unsigned      exact_match:1;