From 257d47f325490dab7fe25df6829ccf2c70f5e0cd Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Sun, 17 Sep 2023 23:19:42 +0200 Subject: [PATCH] refactor: remove `typed_insert` when appending cache-control header it improves the performance of `control_headers` module in general for this particular case. --- .gitignore | 3 ++- benches/Cargo.toml | 15 +++++++++++++++ benches/control_headers.rs | 19 +++++++++++++++++++ src/control_headers.rs | 20 ++++++++++---------- 4 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 benches/Cargo.toml create mode 100644 benches/control_headers.rs diff --git a/.gitignore b/.gitignore index 9278944..708a426 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/target +**/target /bin **/*.rs.bk **/*.log @@ -19,6 +19,7 @@ **/*empty* **/*.json **/*.csv +/benches/Cargo.lock release .vscode TODO diff --git a/benches/Cargo.toml b/benches/Cargo.toml new file mode 100644 index 0000000..a2d0406 --- /dev/null +++ b/benches/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "benches" +version = "0.0.0" +publish = false +edition = "2021" + +[dependencies] +static-web-server = { version = "2", path = "../", features = ["default"] } +criterion = "0.5" +hyper = { version = "0.14", features = ["stream", "http1", "http2", "tcp", "server"] } + +[[bench]] +name = "control_headers" +path = "control_headers.rs" +harness = false diff --git a/benches/control_headers.rs b/benches/control_headers.rs new file mode 100644 index 0000000..4a6c12d --- /dev/null +++ b/benches/control_headers.rs @@ -0,0 +1,19 @@ +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; + +use hyper::{Body, Response, StatusCode}; +use static_web_server::control_headers; + +fn append_headers_benchmark(c: &mut Criterion) { + let mut resp = Response::new(Body::empty()); + *resp.status_mut() = StatusCode::OK; + let uri_path: &str = "assets/image.jpg"; + + c.bench_with_input( + BenchmarkId::new("uri_path_input", uri_path), + &uri_path, + |b, &s| b.iter(|| control_headers::append_headers(s, &mut resp)), + ); +} + +criterion_group!(control_headers_bench, append_headers_benchmark); +criterion_main!(control_headers_bench); diff --git a/src/control_headers.rs b/src/control_headers.rs index 87662cc..7a4c174 100644 --- a/src/control_headers.rs +++ b/src/control_headers.rs @@ -7,7 +7,6 @@ //! for incoming requests based on a set of file types. //! -use headers::{CacheControl, HeaderMapExt}; use hyper::{Body, Response}; // Cache-Control `max-age` variants @@ -36,15 +35,16 @@ pub fn append_headers(uri: &str, resp: &mut Response) { } } - let cache_control = CacheControl::new() - .with_public() - .with_max_age(duration_from_secs(max_age)); - resp.headers_mut().typed_insert(cache_control); -} - -/// It caps a duration value at ~136 years. -fn duration_from_secs(secs: u64) -> std::time::Duration { - std::time::Duration::from_secs(std::cmp::min(secs, u32::MAX as u64)) + resp.headers_mut().insert( + "cache-control", + format!( + "public, max-age={}", + // It caps value in seconds at ~136 years + std::cmp::min(max_age, u32::MAX as u64) + ) + .parse() + .unwrap(), + ); } /// Gets the file extension for a URI. -- libgit2 1.7.2