Skip to content

Commit 1b5fb3c

Browse files
committed
perf(h1): reduce clock checks for date rendering when pipelined
1 parent ba9b1ea commit 1b5fb3c

File tree

4 files changed

+32
-39
lines changed

4 files changed

+32
-39
lines changed

src/proto/h1/date.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ pub const DATE_VALUE_LENGTH: usize = 29;
99

1010
pub fn extend(dst: &mut Vec<u8>) {
1111
CACHED.with(|cache| {
12-
let mut cache = cache.borrow_mut();
13-
let now = time::get_time();
14-
if now > cache.next_update {
15-
cache.update(now);
16-
}
17-
dst.extend_from_slice(cache.buffer());
12+
dst.extend_from_slice(cache.borrow().buffer());
13+
})
14+
}
15+
16+
pub fn update() {
17+
CACHED.with(|cache| {
18+
cache.borrow_mut().check();
1819
})
1920
}
2021

@@ -24,17 +25,30 @@ struct CachedDate {
2425
next_update: time::Timespec,
2526
}
2627

27-
thread_local!(static CACHED: RefCell<CachedDate> = RefCell::new(CachedDate {
28-
bytes: [0; DATE_VALUE_LENGTH],
29-
pos: 0,
30-
next_update: time::Timespec::new(0, 0),
31-
}));
28+
thread_local!(static CACHED: RefCell<CachedDate> = RefCell::new(CachedDate::new()));
3229

3330
impl CachedDate {
31+
fn new() -> Self {
32+
let mut cache = CachedDate {
33+
bytes: [0; DATE_VALUE_LENGTH],
34+
pos: 0,
35+
next_update: time::Timespec::new(0, 0),
36+
};
37+
cache.update(time::get_time());
38+
cache
39+
}
40+
3441
fn buffer(&self) -> &[u8] {
3542
&self.bytes[..]
3643
}
3744

45+
fn check(&mut self) {
46+
let now = time::get_time();
47+
if now > self.next_update {
48+
self.update(now);
49+
}
50+
}
51+
3852
fn update(&mut self, now: time::Timespec) {
3953
self.pos = 0;
4054
let _ = write!(self, "{}", time::at_utc(now).rfc822());

src/proto/h1/dispatch.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ where
8989
}
9090

9191
fn poll_inner(&mut self, should_shutdown: bool) -> Poll<(), ::Error> {
92+
T::update_date();
9293
loop {
9394
self.poll_read()?;
9495
self.poll_write()?;

src/proto/h1/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pub(crate) trait Http1Transaction {
3636

3737
fn should_error_on_parse_eof() -> bool;
3838
fn should_read_first() -> bool;
39+
40+
fn update_date() {}
3941
}
4042

4143
pub(crate) type ParseResult<T> = Result<Option<ParsedMessage<T>>, ::error::Parse>;

src/proto/h1/role.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -182,34 +182,6 @@ where
182182
}))
183183
}
184184

185-
/*
186-
fn decoder(head: &MessageHead<Self::Incoming>, method: &mut Option<Method>) -> ::Result<Decode> {
187-
*method = Some(head.subject.0.clone());
188-
if head.headers.contains_key(TRANSFER_ENCODING) {
189-
// https://tools.ietf.org/html/rfc7230#section-3.3.3
190-
// If Transfer-Encoding header is present, and 'chunked' is
191-
// not the final encoding, and this is a Request, then it is
192-
// mal-formed. A server should respond with 400 Bad Request.
193-
if head.version == Version::HTTP_10 {
194-
debug!("HTTP/1.0 cannot have Transfer-Encoding header");
195-
Err(::Error::new_header())
196-
} else if headers::transfer_encoding_is_chunked(&head.headers) {
197-
Ok(Decode::Normal(Decoder::chunked()))
198-
} else {
199-
debug!("request with transfer-encoding header, but not chunked, bad request");
200-
Err(::Error::new_header())
201-
}
202-
} else if let Some(len) = headers::content_length_parse(&head.headers) {
203-
Ok(Decode::Normal(Decoder::length(len)))
204-
} else if head.headers.contains_key(CONTENT_LENGTH) {
205-
debug!("illegal Content-Length header");
206-
Err(::Error::new_header())
207-
} else {
208-
Ok(Decode::Normal(Decoder::length(0)))
209-
}
210-
}*/
211-
212-
213185
fn encode(mut msg: Encode<Self::Outgoing>, dst: &mut Vec<u8>) -> ::Result<Encoder> {
214186
trace!("Server::encode body={:?}, method={:?}", msg.body, msg.req_method);
215187
debug_assert!(!msg.title_case_headers, "no server config for title case headers");
@@ -464,6 +436,10 @@ where
464436
fn should_read_first() -> bool {
465437
true
466438
}
439+
440+
fn update_date() {
441+
date::update();
442+
}
467443
}
468444

469445
impl Server<()> {

0 commit comments

Comments
 (0)