diff options
author | Alejandro Colomar <alx@nginx.com> | 2023-09-05 03:16:29 +0200 |
---|---|---|
committer | Alejandro Colomar <alx@kernel.org> | 2023-10-25 13:38:57 +0200 |
commit | 5a4e673259b22418dfeb929a1e39ee40427cdd6e (patch) | |
tree | 21f7040eac505a9ea9347849227f18da43233c67 /src/nxt_http_compress_gzip.c | |
parent | 0313eff061f52648f58bea4ec4d40b5255c59226 (diff) | |
download | unit-gzip-v37.tar.gz unit-gzip-v37.tar.bz2 |
HTTP: compress: gzip: calculating wbits and memlevel dynamically.gzip-v37
When the content length is small, optimize zlib for low memory usage.
Conversely, when the content length is large, use a similar amount of
memory within zlib, as it will improve compression, and won't hurt
significantly.
Signed-off-by: Alejandro Colomar <alx@nginx.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Diffstat (limited to 'src/nxt_http_compress_gzip.c')
-rw-r--r-- | src/nxt_http_compress_gzip.c | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/src/nxt_http_compress_gzip.c b/src/nxt_http_compress_gzip.c index f8d88bf8..a95fbf69 100644 --- a/src/nxt_http_compress_gzip.c +++ b/src/nxt_http_compress_gzip.c @@ -9,6 +9,7 @@ #include <stddef.h> #include <stdint.h> +#include <nxt_unit_bit.h> #include <nxt_unit_cdefs.h> #include "nxt_buf.h" @@ -44,7 +45,10 @@ struct nxt_http_compress_gzip_ctx_s { static nxt_http_compress_gzip_ctx_t *nxt_http_compress_gzip_ctx( - nxt_task_t *task, nxt_http_request_t *r, nxt_http_compress_conf_t *conf); + nxt_task_t *task, nxt_http_request_t *r, nxt_http_compress_conf_t *conf, + size_t content_length); +static int nxt_http_compress_gzip_memlevel(size_t content_length); +static int nxt_http_compress_gzip_wbits(size_t content_length); static void nxt_http_compress_gzip_filter(nxt_task_t *task, void *obj, void *data); @@ -95,7 +99,7 @@ nxt_http_compress_gzip(nxt_task_t *task, nxt_http_request_t *r, return NXT_ERROR; } - ctx = nxt_http_compress_gzip_ctx(task, r, conf); + ctx = nxt_http_compress_gzip_ctx(task, r, conf, clen); if (nxt_slow_path(ctx == NULL)) { return NXT_ERROR; } @@ -111,7 +115,7 @@ nxt_http_compress_gzip(nxt_task_t *task, nxt_http_request_t *r, static nxt_http_compress_gzip_ctx_t * nxt_http_compress_gzip_ctx(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_compress_conf_t *conf) + nxt_http_compress_conf_t *conf, size_t content_length) { #if (!NXT_WITH_ZLIB) return NULL; @@ -132,7 +136,9 @@ nxt_http_compress_gzip_ctx(nxt_task_t *task, nxt_http_request_t *r, z->zalloc = NULL; z->zfree = NULL; z->opaque = NULL; - ret = deflateInit2(z, ctx->level, Z_DEFLATED, MAX_WBITS + 16, MAX_MEM_LEVEL, + ret = deflateInit2(z, ctx->level, Z_DEFLATED, + nxt_http_compress_gzip_wbits(content_length), + nxt_http_compress_gzip_memlevel(content_length), Z_DEFAULT_STRATEGY); if (nxt_slow_path(ret != 0)) { return NULL; @@ -143,6 +149,28 @@ nxt_http_compress_gzip_ctx(nxt_task_t *task, nxt_http_request_t *r, } +static int +nxt_http_compress_gzip_memlevel(size_t content_length) +{ + int w; + + w = nxt_bit_width_ul(content_length); + + return nxt_min(MAX_MEM_LEVEL, nxt_max(2, w - 9)); +} + + +static int +nxt_http_compress_gzip_wbits(size_t content_length) +{ + int w; + + w = nxt_bit_width_ul(content_length); + + return 16 + nxt_min(MAX_WBITS, nxt_max(9, w - 2)); +} + + static void nxt_http_compress_gzip_filter(nxt_task_t *task, void *obj, void *data) { |