summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAlejandro Colomar <alx@nginx.com>2023-09-05 03:16:29 +0200
committerAlejandro Colomar <alx@kernel.org>2023-12-19 15:02:42 +0100
commit9479559dc08ab13adc4c60e198d5e4d9d289ee02 (patch)
tree52648165772e01d3e0227e34f7531d7190779fe2
parentf9761247794fce2de3726f3de9871a8ec24dcef3 (diff)
downloadunit-9479559dc08ab13adc4c60e198d5e4d9d289ee02.tar.gz
unit-9479559dc08ab13adc4c60e198d5e4d9d289ee02.tar.bz2
HTTP: compress: gzip: calculating wbits and memlevel dynamically.alx-gzip
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>
-rw-r--r--src/nxt_http_compress_gzip.c36
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)
{