[nginx] Saved some memory allocations.

Ruslan Ermilov ru at nginx.com
Mon Dec 23 15:45:43 UTC 2019


details:   https://hg.nginx.org/nginx/rev/b399246ea45d
branches:  
changeset: 7602:b399246ea45d
user:      Ruslan Ermilov <ru at nginx.com>
date:      Mon Dec 16 15:19:01 2019 +0300
description:
Saved some memory allocations.

In configurations when "root" has variables, some modules unnecessarily
allocated memory for the "Location" header value.

diffstat:

 src/http/modules/ngx_http_dav_module.c    |  31 +++++--------------------------
 src/http/modules/ngx_http_static_module.c |   4 ++--
 2 files changed, 7 insertions(+), 28 deletions(-)

diffs (84 lines):

diff -r c1a7d3672653 -r b399246ea45d src/http/modules/ngx_http_dav_module.c
--- a/src/http/modules/ngx_http_dav_module.c	Mon Dec 16 15:19:01 2019 +0300
+++ b/src/http/modules/ngx_http_dav_module.c	Mon Dec 16 15:19:01 2019 +0300
@@ -56,7 +56,7 @@
 static ngx_int_t ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt);
 static ngx_int_t ngx_http_dav_error(ngx_log_t *log, ngx_err_t err,
     ngx_int_t not_found, char *failed, u_char *path);
-static ngx_int_t ngx_http_dav_location(ngx_http_request_t *r, u_char *path);
+static ngx_int_t ngx_http_dav_location(ngx_http_request_t *r);
 static void *ngx_http_dav_create_loc_conf(ngx_conf_t *cf);
 static char *ngx_http_dav_merge_loc_conf(ngx_conf_t *cf,
     void *parent, void *child);
@@ -285,7 +285,7 @@
     }
 
     if (status == NGX_HTTP_CREATED) {
-        if (ngx_http_dav_location(r, path.data) != NGX_OK) {
+        if (ngx_http_dav_location(r) != NGX_OK) {
             ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
             return;
         }
@@ -520,9 +520,7 @@
     if (ngx_create_dir(path.data, ngx_dir_access(dlcf->access))
         != NGX_FILE_ERROR)
     {
-        *(p - 1) = '/';
-
-        if (ngx_http_dav_location(r, path.data) != NGX_OK) {
+        if (ngx_http_dav_location(r) != NGX_OK) {
             return NGX_HTTP_INTERNAL_SERVER_ERROR;
         }
 
@@ -1070,35 +1068,16 @@
 
 
 static ngx_int_t
-ngx_http_dav_location(ngx_http_request_t *r, u_char *path)
+ngx_http_dav_location(ngx_http_request_t *r)
 {
-    u_char                    *location;
-    ngx_http_core_loc_conf_t  *clcf;
-
     r->headers_out.location = ngx_list_push(&r->headers_out.headers);
     if (r->headers_out.location == NULL) {
         return NGX_ERROR;
     }
 
-    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
-
-    if (!clcf->alias && clcf->root_lengths == NULL) {
-        location = path + clcf->root.len;
-
-    } else {
-        location = ngx_pnalloc(r->pool, r->uri.len);
-        if (location == NULL) {
-            ngx_http_clear_location(r);
-            return NGX_ERROR;
-        }
-
-        ngx_memcpy(location, r->uri.data, r->uri.len);
-    }
-
     r->headers_out.location->hash = 1;
     ngx_str_set(&r->headers_out.location->key, "Location");
-    r->headers_out.location->value.len = r->uri.len;
-    r->headers_out.location->value.data = location;
+    r->headers_out.location->value = r->uri;
 
     return NGX_OK;
 }
diff -r c1a7d3672653 -r b399246ea45d src/http/modules/ngx_http_static_module.c
--- a/src/http/modules/ngx_http_static_module.c	Mon Dec 16 15:19:01 2019 +0300
+++ b/src/http/modules/ngx_http_static_module.c	Mon Dec 16 15:19:01 2019 +0300
@@ -157,8 +157,8 @@
 
         len = r->uri.len + 1;
 
-        if (!clcf->alias && clcf->root_lengths == NULL && r->args.len == 0) {
-            location = path.data + clcf->root.len;
+        if (!clcf->alias && r->args.len == 0) {
+            location = path.data + root;
 
             *last = '/';
 


More information about the nginx-devel mailing list