Skip to content

Commit

Permalink
perf(ext/http): faster accept-encoding parsing (#14654)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronO authored May 18, 2022
1 parent c4b7bdb commit f2410b4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
14 changes: 12 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ext/http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ cache_control = "0.2.0"
deno_core = { version = "0.134.0", path = "../../core" }
deno_websocket = { version = "0.57.0", path = "../websocket" }
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"] }
mime = "0.3.16"
percent-encoding = "2.1.0"
Expand Down
36 changes: 11 additions & 25 deletions ext/http/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,31 +390,17 @@ async fn op_http_accept(
_ => unreachable!(),
};

{
let mut accept_encoding = stream.accept_encoding.borrow_mut();

// curl --compressed sends "Accept-Encoding: deflate, gzip".
// fly_accept_encoding::parse() returns Encoding::Deflate.
// Deno does not support Encoding::Deflate.
// So, Deno used no compression, although gzip was possible.
// 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;
}
}
}
}
}
stream.accept_encoding.replace({
let encodings = fly_accept_encoding::encodings_iter(request.headers())
.filter(|r| {
matches!(r, Ok((Some(Encoding::Brotli | Encoding::Gzip), _)))
});

fly_accept_encoding::preferred(encodings)
.ok()
.flatten()
.unwrap_or(Encoding::Identity)
});

let method = request.method().to_string();
let headers = req_headers(request);
Expand Down

0 comments on commit f2410b4

Please sign in to comment.