1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

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
```
This commit is contained in:
Matt Mastracci 2023-07-07 10:46:56 -06:00 committed by GitHub
parent 75d2c045f7
commit 7d022ad11a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,6 +7,7 @@ use std::pin::Pin;
use std::rc::Rc; use std::rc::Rc;
use std::task::Waker; use std::task::Waker;
use brotli::enc::encode::BrotliEncoderParameter;
use brotli::ffi::compressor::BrotliEncoderState; use brotli::ffi::compressor::BrotliEncoderState;
use bytes::Bytes; use bytes::Bytes;
use bytes::BytesMut; use bytes::BytesMut;
@ -613,17 +614,30 @@ impl Drop for BrotliEncoderStateWrapper {
impl BrotliResponseStream { impl BrotliResponseStream {
pub fn new(underlying: ResponseStream) -> Self { 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 { Self {
// SAFETY: creating an FFI instance should be OK with these args. stm,
stm: unsafe {
BrotliEncoderStateWrapper {
stm: brotli::ffi::compressor::BrotliEncoderCreateInstance(
None,
None,
std::ptr::null_mut(),
),
}
},
output_written_so_far: 0, output_written_so_far: 0,
current_cursor: 0, current_cursor: 0,
state: BrotliState::Streaming, state: BrotliState::Streaming,