mirror of https://github.com/fafhrd91/actix-web
perf: remove unnecessary allocation when writing http dates
This commit is contained in:
parent
a7375b6876
commit
635ea464f4
|
@ -57,6 +57,8 @@ jobs:
|
||||||
- name: workaround MSRV issues
|
- name: workaround MSRV issues
|
||||||
if: matrix.version.name == 'msrv'
|
if: matrix.version.name == 'msrv'
|
||||||
run: |
|
run: |
|
||||||
|
cargo update -p=ciborium --precise=0.2.1
|
||||||
|
cargo update -p=ciborium-ll --precise=0.2.1
|
||||||
cargo update -p=clap --precise=4.3.24
|
cargo update -p=clap --precise=4.3.24
|
||||||
cargo update -p=clap_lex --precise=0.5.0
|
cargo update -p=clap_lex --precise=0.5.0
|
||||||
cargo update -p=anstyle --precise=1.0.2
|
cargo update -p=anstyle --precise=1.0.2
|
||||||
|
|
|
@ -113,6 +113,7 @@ actix-web = "4"
|
||||||
|
|
||||||
async-stream = "0.3"
|
async-stream = "0.3"
|
||||||
criterion = { version = "0.5", features = ["html_reports"] }
|
criterion = { version = "0.5", features = ["html_reports"] }
|
||||||
|
divan = "0.1.8"
|
||||||
env_logger = "0.10"
|
env_logger = "0.10"
|
||||||
futures-util = { version = "0.3.17", default-features = false, features = ["alloc"] }
|
futures-util = { version = "0.3.17", default-features = false, features = ["alloc"] }
|
||||||
memchr = "2.4"
|
memchr = "2.4"
|
||||||
|
@ -140,3 +141,7 @@ required-features = ["http2", "rustls-0_21"]
|
||||||
name = "response-body-compression"
|
name = "response-body-compression"
|
||||||
harness = false
|
harness = false
|
||||||
required-features = ["compress-brotli", "compress-gzip", "compress-zstd"]
|
required-features = ["compress-brotli", "compress-gzip", "compress-zstd"]
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "date-formatting"
|
||||||
|
harness = false
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
use std::time::SystemTime;
|
||||||
|
|
||||||
|
use actix_http::header::HttpDate;
|
||||||
|
use divan::{black_box, AllocProfiler, Bencher};
|
||||||
|
|
||||||
|
#[global_allocator]
|
||||||
|
static ALLOC: AllocProfiler = AllocProfiler::system();
|
||||||
|
|
||||||
|
#[divan::bench]
|
||||||
|
fn date_formatting(b: Bencher<'_, '_>) {
|
||||||
|
let now = SystemTime::now();
|
||||||
|
|
||||||
|
b.bench(|| {
|
||||||
|
black_box(HttpDate::from(black_box(now)).to_string());
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
divan::main();
|
||||||
|
}
|
|
@ -28,7 +28,7 @@ impl Date {
|
||||||
|
|
||||||
fn update(&mut self) {
|
fn update(&mut self) {
|
||||||
self.pos = 0;
|
self.pos = 0;
|
||||||
write!(self, "{}", httpdate::fmt_http_date(SystemTime::now())).unwrap();
|
write!(self, "{}", httpdate::HttpDate::from(SystemTime::now())).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,8 +24,7 @@ impl FromStr for HttpDate {
|
||||||
|
|
||||||
impl fmt::Display for HttpDate {
|
impl fmt::Display for HttpDate {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
let date_str = httpdate::fmt_http_date(self.0);
|
httpdate::HttpDate::from(self.0).fmt(f)
|
||||||
f.write_str(&date_str)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,7 +36,7 @@ impl TryIntoHeaderValue for HttpDate {
|
||||||
let mut wrt = MutWriter(&mut buf);
|
let mut wrt = MutWriter(&mut buf);
|
||||||
|
|
||||||
// unwrap: date output is known to be well formed and of known length
|
// unwrap: date output is known to be well formed and of known length
|
||||||
write!(wrt, "{}", httpdate::fmt_http_date(self.0)).unwrap();
|
write!(wrt, "{}", self).unwrap();
|
||||||
|
|
||||||
HeaderValue::from_maybe_shared(buf.split().freeze())
|
HeaderValue::from_maybe_shared(buf.split().freeze())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue