diff options
author | Matt Mastracci <matthew@mastracci.com> | 2023-07-07 10:46:56 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-07 10:46:56 -0600 |
commit | 7d022ad11a74710ce46e4ab9f4e57635cae3ed2e (patch) | |
tree | ff29187b7d99d5588596c2730a7603793d181346 /ext/http/response_body.rs | |
parent | 75d2c045f79aee4f772cb235860e6ef340177a05 (diff) |
fix(ext/http): Use brotli compression params (#19758)
Fixes #19737 by adding brotli compression parameters.
Time after:
`Accept-Encoding: gzip`:
```
real 0m0.214s
user 0m0.005s
sys 0m0.013s
```
`Accept-Encoding: br`:
Before:
```
real 0m10.303s
user 0m0.005s
sys 0m0.010s
```
After:
```
real 0m0.127s
user 0m0.006s
sys 0m0.014s
```
Diffstat (limited to 'ext/http/response_body.rs')
-rw-r--r-- | ext/http/response_body.rs | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/ext/http/response_body.rs b/ext/http/response_body.rs index 6793f0e78..3697b2732 100644 --- a/ext/http/response_body.rs +++ b/ext/http/response_body.rs @@ -7,6 +7,7 @@ use std::pin::Pin; use std::rc::Rc; use std::task::Waker; +use brotli::enc::encode::BrotliEncoderParameter; use brotli::ffi::compressor::BrotliEncoderState; use bytes::Bytes; use bytes::BytesMut; @@ -613,17 +614,30 @@ impl Drop for BrotliEncoderStateWrapper { impl BrotliResponseStream { pub fn new(underlying: ResponseStream) -> Self { + // SAFETY: creating an FFI instance should be OK with these args. + let stm = unsafe { + let stm = brotli::ffi::compressor::BrotliEncoderCreateInstance( + None, + None, + std::ptr::null_mut(), + ); + // Quality level 6 is based on google's nginx default value for on-the-fly compression + // https://github.com/google/ngx_brotli#brotli_comp_level + // lgwin 22 is equivalent to brotli window size of (2**22)-16 bytes (~4MB) + brotli::ffi::compressor::BrotliEncoderSetParameter( + stm, + BrotliEncoderParameter::BROTLI_PARAM_QUALITY, + 6, + ); + brotli::ffi::compressor::BrotliEncoderSetParameter( + stm, + BrotliEncoderParameter::BROTLI_PARAM_LGWIN, + 22, + ); + BrotliEncoderStateWrapper { stm } + }; Self { - // SAFETY: creating an FFI instance should be OK with these args. - stm: unsafe { - BrotliEncoderStateWrapper { - stm: brotli::ffi::compressor::BrotliEncoderCreateInstance( - None, - None, - std::ptr::null_mut(), - ), - } - }, + stm, output_written_so_far: 0, current_cursor: 0, state: BrotliState::Streaming, |