Free unused large block when doubling capacity of ngx_array_t.

Yousong Zhou yszhou4tech at gmail.com
Fri Jul 12 11:10:33 UTC 2013


# HG changeset patch
# User Yousong Zhou <yszhou4tech at gmail.com>
# Date 1373358378 -28800
# Branch t
# Node ID b8a53d0bb5c306b89eef767694fcf127a0da8f41
# Parent  626f288fa5ede7ee3cbeffe950cb9dd611e10c52
Free unused large block when doubling capacity of ngx_array_t.

When `nalloc' of array needs to be doubled, originally allocated large
block should be freed. This can lead to gains from 2 aspects:

 - It saves unnessary memory consumption.
 - Length of p->large chain could be constrained a little to reduce
   transversal cost.

diff -r 626f288fa5ed -r b8a53d0bb5c3 src/core/ngx_array.c
--- a/src/core/ngx_array.c	Fri Jul 05 11:42:25 2013 +0400
+++ b/src/core/ngx_array.c	Tue Jul 09 16:26:18 2013 +0800
@@ -79,6 +79,7 @@
             }
 
             ngx_memcpy(new, a->elts, size);
+            ngx_pfree(p, a->elts);
             a->elts = new;
             a->nalloc *= 2;
         }
@@ -129,6 +130,7 @@
             }
 
             ngx_memcpy(new, a->elts, a->nelts * a->size);
+            ngx_pfree(p, a->elts);
             a->elts = new;
             a->nalloc = nalloc;
         }



More information about the nginx-devel mailing list