Skip to content

Commit

Permalink
perf: remove unnecessary allocation when writing http dates
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Jan 24, 2024
1 parent a7375b6 commit 635ea46
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ jobs:
- name: workaround MSRV issues
if: matrix.version.name == 'msrv'
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_lex --precise=0.5.0
cargo update -p=anstyle --precise=1.0.2
Expand Down
5 changes: 5 additions & 0 deletions actix-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ actix-web = "4"

async-stream = "0.3"
criterion = { version = "0.5", features = ["html_reports"] }
divan = "0.1.8"
env_logger = "0.10"
futures-util = { version = "0.3.17", default-features = false, features = ["alloc"] }
memchr = "2.4"
Expand Down Expand Up @@ -140,3 +141,7 @@ required-features = ["http2", "rustls-0_21"]
name = "response-body-compression"
harness = false
required-features = ["compress-brotli", "compress-gzip", "compress-zstd"]

[[bench]]
name = "date-formatting"
harness = false
20 changes: 20 additions & 0 deletions actix-http/benches/date-formatting.rs
Original file line number Diff line number Diff line change
@@ -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();
}
2 changes: 1 addition & 1 deletion actix-http/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Date {

fn update(&mut self) {
self.pos = 0;
write!(self, "{}", httpdate::fmt_http_date(SystemTime::now())).unwrap();
write!(self, "{}", httpdate::HttpDate::from(SystemTime::now())).unwrap();
}
}

Expand Down
5 changes: 2 additions & 3 deletions actix-http/src/header/shared/http_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ impl FromStr for HttpDate {

impl fmt::Display for HttpDate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let date_str = httpdate::fmt_http_date(self.0);
f.write_str(&date_str)
httpdate::HttpDate::from(self.0).fmt(f)
}
}

Expand All @@ -37,7 +36,7 @@ impl TryIntoHeaderValue for HttpDate {
let mut wrt = MutWriter(&mut buf);

// 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())
}
Expand Down

0 comments on commit 635ea46

Please sign in to comment.