change growth factor of array

Maxim Dounin mdounin at mdounin.ru
Fri Oct 19 13:25:06 UTC 2012


Hello!

On Fri, Oct 19, 2012 at 08:06:15PM +0800, Simon Liu wrote:

> In this document (
> https://github.com/facebook/folly/blob/master/folly/docs/FBVector.md ) ,
>  It is to suggest use factor 1.5 (when you'd push into a array without
> there being room)  in dynamically-allocated arrays. the factor is 2 in
> array of Nginx, and so I think may be change factor to 1.5 is be better in
> Nginx's array.

While the reasoning about memory reuse provided in the document in 
question looks valid from memory point of view, it doesn't really 
apply to nginx arrays due to the following reasons:

- Previously used memory isn't freed anyway (instead nginx rely on 
  pool allocator to free it on pool destruction, plus in some 
  cases it's on-stack memory).

- Arrays are used for usually small data sets which are [almost] 
  stable.  If use case suggests dynamic growth - lists are used 
  instead (see src/core/ngx_list.c).

On the other hand, 2 is better than 1.5 as it results in less 
reallocation operations on average.

-- 
Maxim Dounin
http://nginx.com/support.html



More information about the nginx-devel mailing list