From 599edb49a3dceab40315fa3345d9d802c4b44597 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 29 Jan 2020 10:04:36 +0100 Subject: [PATCH] src/fut_with_diag: Reduce overhead when logging disabled `DiagnoeFuture::poll` either just polls the underlying future when logging is disabled, or records the start time, polls the future, records the end time, logs the information and then returns the poll outcome. In the former case where logging is disabled, there is no reason to record start and end time, given that `log_poll` becomes a no-op. --- src/fut_with_diag.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/fut_with_diag.rs b/src/fut_with_diag.rs index 7f31927..1548378 100644 --- a/src/fut_with_diag.rs +++ b/src/fut_with_diag.rs @@ -61,19 +61,22 @@ where fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll { let this = self.project(); + if !log_out::is_enabled() { + return Future::poll(this.inner, cx); + } + let before = Instant::now(); - let outcome = if log_out::is_enabled() { - let waker = ctxt_with_diag::waker_with_diag( - cx.waker().clone(), - this.task_name.clone(), - *this.task_id, - ); - let mut cx = Context::from_waker(&waker); - Future::poll(this.inner, &mut cx) - } else { - Future::poll(this.inner, cx) - }; + + let waker = ctxt_with_diag::waker_with_diag( + cx.waker().clone(), + this.task_name.clone(), + *this.task_id, + ); + let mut cx = Context::from_waker(&waker); + let outcome = Future::poll(this.inner, &mut cx); + let after = Instant::now(); + log_out::log_poll( &this.task_name, *this.task_id, @@ -82,6 +85,7 @@ where mem::replace(this.first_time_poll, false), outcome.is_ready(), ); + outcome } }