comparison src/http/modules/ngx_http_gzip_filter_module.c @ 7817:c297c2c252d8

Gzip: updated handling of zlib variant from Intel. In current versions (all versions based on zlib 1.2.11, at least since 2018) it no longer uses 64K hash and does not force window bits to 13 if it is less than 13. That is, it needs just 16 bytes more memory than normal zlib, so these bytes are simply added to the normal size calculation.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 05 Apr 2021 04:07:17 +0300
parents 1f3d0d9f893f
children d26db4f82d7d
comparison
equal deleted inserted replaced
7816:1f3d0d9f893f 7817:c297c2c252d8
54 unsigned flush:4; 54 unsigned flush:4;
55 unsigned redo:1; 55 unsigned redo:1;
56 unsigned done:1; 56 unsigned done:1;
57 unsigned nomem:1; 57 unsigned nomem:1;
58 unsigned buffering:1; 58 unsigned buffering:1;
59 unsigned intel:1;
60 unsigned zlib_ng:1; 59 unsigned zlib_ng:1;
61 60
62 size_t zin; 61 size_t zin;
63 size_t zout; 62 size_t zout;
64 63
212 static ngx_str_t ngx_http_gzip_ratio = ngx_string("gzip_ratio"); 211 static ngx_str_t ngx_http_gzip_ratio = ngx_string("gzip_ratio");
213 212
214 static ngx_http_output_header_filter_pt ngx_http_next_header_filter; 213 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
215 static ngx_http_output_body_filter_pt ngx_http_next_body_filter; 214 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
216 215
217 static ngx_uint_t ngx_http_gzip_assume_intel;
218 static ngx_uint_t ngx_http_gzip_assume_zlib_ng; 216 static ngx_uint_t ngx_http_gzip_assume_zlib_ng;
219 217
220 218
221 static ngx_int_t 219 static ngx_int_t
222 ngx_http_gzip_header_filter(ngx_http_request_t *r) 220 ngx_http_gzip_header_filter(ngx_http_request_t *r)
501 * and do not wait while a whole response will be sent to a client. 499 * and do not wait while a whole response will be sent to a client.
502 * 500 *
503 * 8K is for zlib deflate_state, it takes 501 * 8K is for zlib deflate_state, it takes
504 * *) 5816 bytes on i386 and sparc64 (32-bit mode) 502 * *) 5816 bytes on i386 and sparc64 (32-bit mode)
505 * *) 5920 bytes on amd64 and sparc64 503 * *) 5920 bytes on amd64 and sparc64
504 *
505 * A zlib variant from Intel (https://github.com/jtkukunas/zlib)
506 * uses additional 16-byte padding in one of window-sized buffers.
506 */ 507 */
507 508
508 if (!ngx_http_gzip_assume_intel) { 509 if (!ngx_http_gzip_assume_zlib_ng) {
509 ctx->allocated = 8192 + (1 << (wbits + 2)) + (1 << (memlevel + 9));
510
511 } else if (!ngx_http_gzip_assume_zlib_ng) {
512 /*
513 * A zlib variant from Intel, https://github.com/jtkukunas/zlib.
514 * It can force window bits to 13 for fast compression level,
515 * on processors with SSE 4.2 it uses 64K hash instead of scaling
516 * it from the specified memory level, and also introduces
517 * 16-byte padding in one out of the two window-sized buffers.
518 */
519
520 if (conf->level == 1) {
521 wbits = ngx_max(wbits, 13);
522 }
523
524 ctx->allocated = 8192 + 16 + (1 << (wbits + 2)) 510 ctx->allocated = 8192 + 16 + (1 << (wbits + 2))
525 + (1 << (ngx_max(memlevel, 8) + 8)) 511 + (1 << (memlevel + 9));
526 + (1 << (memlevel + 8));
527 ctx->intel = 1;
528 512
529 } else { 513 } else {
530 /* 514 /*
531 * Another zlib variant, https://github.com/zlib-ng/zlib-ng. 515 * Another zlib variant, https://github.com/zlib-ng/zlib-ng.
532 * Similar to Intel's variant, though uses 128K hash. 516 * It forces window bits to 13 for fast compression level,
517 * uses 16-byte padding in one of window-sized buffers, and
518 * uses 128K hash.
533 */ 519 */
534 520
535 if (conf->level == 1) { 521 if (conf->level == 1) {
536 wbits = ngx_max(wbits, 13); 522 wbits = ngx_max(wbits, 13);
537 } 523 }
964 if (ctx->zlib_ng) { 950 if (ctx->zlib_ng) {
965 ngx_log_error(NGX_LOG_ALERT, ctx->request->connection->log, 0, 951 ngx_log_error(NGX_LOG_ALERT, ctx->request->connection->log, 0,
966 "gzip filter failed to use preallocated memory: " 952 "gzip filter failed to use preallocated memory: "
967 "%ud of %ui", items * size, ctx->allocated); 953 "%ud of %ui", items * size, ctx->allocated);
968 954
969 } else if (ctx->intel) { 955 } else {
970 ngx_http_gzip_assume_zlib_ng = 1; 956 ngx_http_gzip_assume_zlib_ng = 1;
971
972 } else {
973 ngx_http_gzip_assume_intel = 1;
974 } 957 }
975 958
976 p = ngx_palloc(ctx->request->pool, items * size); 959 p = ngx_palloc(ctx->request->pool, items * size);
977 960
978 return p; 961 return p;