[PATCH] HTTP/2: premalloc http2 recv_buffer in init_conf

Maxim Dounin mdounin at mdounin.ru
Mon Sep 7 13:25:15 UTC 2020


Hello!

On Mon, Sep 07, 2020 at 08:06:06PM +0800, Kasei Wang wrote:

> # HG changeset patch
> # User Kasei Wang <kasei at kasei.im>
> # Date 1599480224 -28800
> #      Mon Sep 07 20:03:44 2020 +0800
> # Node ID 1532a8fca7b24c3c3aa5cddd3362402d15f57a4f
> # Parent  87d2ea860f380dc8418c97c0163412f53c2d008e
> HTTP/2: premalloc http2 recv_buffer in init_conf
> 
> There is no need to check if h2mcf->recv_buffer malloced every time.
> 
> diff -r 87d2ea860f38 -r 1532a8fca7b2 src/http/v2/ngx_http_v2.c
> --- a/src/http/v2/ngx_http_v2.c    Mon Sep 10 18:57:39 2018 +0300
> +++ b/src/http/v2/ngx_http_v2.c    Mon Sep 07 20:03:44 2020 +0800
> @@ -233,7 +233,6 @@
>      ngx_pool_cleanup_t        *cln;
>      ngx_http_connection_t     *hc;
>      ngx_http_v2_srv_conf_t    *h2scf;
> -    ngx_http_v2_main_conf_t   *h2mcf;
>      ngx_http_v2_connection_t  *h2c;
> 
>      c = rev->data;
> @@ -243,17 +242,6 @@
> 
>      c->log->action = "processing HTTP/2 connection";
> 
> -    h2mcf = ngx_http_get_module_main_conf(hc->conf_ctx, ngx_http_v2_module);
> -
> -    if (h2mcf->recv_buffer == NULL) {
> -        h2mcf->recv_buffer = ngx_palloc(ngx_cycle->pool,
> -                                        h2mcf->recv_buffer_size);
> -        if (h2mcf->recv_buffer == NULL) {
> -            ngx_http_close_connection(c);
> -            return;
> -        }
> -    }
> -
>      h2c = ngx_pcalloc(c->pool, sizeof(ngx_http_v2_connection_t));
>      if (h2c == NULL) {
>          ngx_http_close_connection(c);
> diff -r 87d2ea860f38 -r 1532a8fca7b2 src/http/v2/ngx_http_v2_module.c
> --- a/src/http/v2/ngx_http_v2_module.c    Mon Sep 10 18:57:39 2018 +0300
> +++ b/src/http/v2/ngx_http_v2_module.c    Mon Sep 07 20:03:44 2020 +0800
> @@ -334,6 +334,10 @@
>      ngx_http_v2_main_conf_t *h2mcf = conf;
> 
>      ngx_conf_init_size_value(h2mcf->recv_buffer_size, 256 * 1024);
> +    h2mcf->recv_buffer = ngx_pcalloc(cf->pool, h2mcf->recv_buffer_size);
> +    if (h2mcf->recv_buffer == NULL) {
> +        return NGX_CONF_ERROR;
> +    }
> 
>      return NGX_CONF_OK;
>  }

Thank you for the patch.

While current approach implies some run-time costs, these costs 
are believed to be very small and not even measurable.  On the 
other hand, it saves relatively large buffer allocation if HTTP/2 
is compiled in but not used, and this might be important in some 
setups.

-- 
Maxim Dounin
http://mdounin.ru/


More information about the nginx-devel mailing list