Skip to content

Commit 29a8331

Browse files
committed
perf: remove unnecessary allocation when writing http dates
1 parent a7375b6 commit 29a8331

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

.github/workflows/ci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ jobs:
5757
- name: workaround MSRV issues
5858
if: matrix.version.name == 'msrv'
5959
run: |
60+
cargo update -p=ciborium --precise=0.2.1
61+
cargo update -p=ciborium-ll --precise=0.2.1
6062
cargo update -p=clap --precise=4.3.24
6163
cargo update -p=clap_lex --precise=0.5.0
6264
cargo update -p=anstyle --precise=1.0.2

actix-http/Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ actix-web = "4"
113113

114114
async-stream = "0.3"
115115
criterion = { version = "0.5", features = ["html_reports"] }
116+
divan = "0.1.11"
116117
env_logger = "0.10"
117118
futures-util = { version = "0.3.17", default-features = false, features = ["alloc"] }
118119
memchr = "2.4"
@@ -140,3 +141,7 @@ required-features = ["http2", "rustls-0_21"]
140141
name = "response-body-compression"
141142
harness = false
142143
required-features = ["compress-brotli", "compress-gzip", "compress-zstd"]
144+
145+
[[bench]]
146+
name = "date-formatting"
147+
harness = false

actix-http/benches/date-formatting.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::time::SystemTime;
2+
3+
use actix_http::header::HttpDate;
4+
use divan::{black_box, AllocProfiler, Bencher};
5+
6+
#[global_allocator]
7+
static ALLOC: AllocProfiler = AllocProfiler::system();
8+
9+
#[divan::bench]
10+
fn date_formatting(b: Bencher<'_, '_>) {
11+
let now = SystemTime::now();
12+
13+
b.bench(|| {
14+
black_box(HttpDate::from(black_box(now)).to_string());
15+
})
16+
}
17+
18+
fn main() {
19+
divan::main();
20+
}

actix-http/src/date.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl Date {
2828

2929
fn update(&mut self) {
3030
self.pos = 0;
31-
write!(self, "{}", httpdate::fmt_http_date(SystemTime::now())).unwrap();
31+
write!(self, "{}", httpdate::HttpDate::from(SystemTime::now())).unwrap();
3232
}
3333
}
3434

actix-http/src/header/shared/http_date.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ impl FromStr for HttpDate {
2424

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

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

3938
// unwrap: date output is known to be well formed and of known length
40-
write!(wrt, "{}", httpdate::fmt_http_date(self.0)).unwrap();
39+
write!(wrt, "{}", self).unwrap();
4140

4241
HeaderValue::from_maybe_shared(buf.split().freeze())
4342
}

0 commit comments

Comments
 (0)