1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-30 16:40:57 -05:00

perf(ext/http): faster accept-encoding parsing (#14654)

This commit is contained in:
Aaron O'Mullan 2022-05-18 16:14:23 +02:00 committed by GitHub
parent c4b7bdb9d1
commit f2410b4481
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 27 deletions

14
Cargo.lock generated
View file

@ -1575,11 +1575,12 @@ dependencies = [
[[package]] [[package]]
name = "fly-accept-encoding" name = "fly-accept-encoding"
version = "0.2.0-alpha.5" version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "741d3e4ac3bcebc022cd90e7d1ce376515a73db2d53ba7fd3a7e581d6db7fa97" checksum = "a3afa7516fdcfd8e5e93a938f8fec857785ced190a1f62d842d1fe1ffbe22ba8"
dependencies = [ dependencies = [
"http", "http",
"itertools",
"thiserror", "thiserror",
] ]
@ -2138,6 +2139,15 @@ dependencies = [
"syn 1.0.93", "syn 1.0.93",
] ]
[[package]]
name = "itertools"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.1" version = "1.0.1"

View file

@ -26,7 +26,7 @@ cache_control = "0.2.0"
deno_core = { version = "0.134.0", path = "../../core" } deno_core = { version = "0.134.0", path = "../../core" }
deno_websocket = { version = "0.57.0", path = "../websocket" } deno_websocket = { version = "0.57.0", path = "../websocket" }
flate2 = "1.0.23" flate2 = "1.0.23"
fly-accept-encoding = "0.2.0-alpha.5" fly-accept-encoding = "0.2.0"
hyper = { version = "0.14.18", features = ["server", "stream", "http1", "http2", "runtime"] } hyper = { version = "0.14.18", features = ["server", "stream", "http1", "http2", "runtime"] }
mime = "0.3.16" mime = "0.3.16"
percent-encoding = "2.1.0" percent-encoding = "2.1.0"

View file

@ -390,31 +390,17 @@ async fn op_http_accept(
_ => unreachable!(), _ => unreachable!(),
}; };
{ stream.accept_encoding.replace({
let mut accept_encoding = stream.accept_encoding.borrow_mut(); let encodings = fly_accept_encoding::encodings_iter(request.headers())
.filter(|r| {
matches!(r, Ok((Some(Encoding::Brotli | Encoding::Gzip), _)))
});
// curl --compressed sends "Accept-Encoding: deflate, gzip". fly_accept_encoding::preferred(encodings)
// fly_accept_encoding::parse() returns Encoding::Deflate. .ok()
// Deno does not support Encoding::Deflate. .flatten()
// So, Deno used no compression, although gzip was possible. .unwrap_or(Encoding::Identity)
// This patch makes Deno use gzip instead in this case. });
*accept_encoding = Encoding::Identity;
let mut max_qval = 0.0;
if let Ok(encodings) = fly_accept_encoding::encodings(request.headers()) {
for (encoding, qval) in encodings {
if let Some(enc @ (Encoding::Brotli | Encoding::Gzip)) = encoding {
// this logic came from fly_accept_encoding.
if (qval - 1.0f32).abs() < 0.01 {
*accept_encoding = enc;
break;
} else if qval > max_qval {
*accept_encoding = enc;
max_qval = qval;
}
}
}
}
}
let method = request.method().to_string(); let method = request.method().to_string();
let headers = req_headers(request); let headers = req_headers(request);