From b480b3e2385815e810db911b091c424a2a8a6d85 Mon Sep 17 00:00:00 2001 From: mox692 Date: Thu, 27 Feb 2025 23:22:53 +0900 Subject: [PATCH] runtime: avoid `powf` in the hotpath --- tokio/src/runtime/scheduler/multi_thread/stats.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tokio/src/runtime/scheduler/multi_thread/stats.rs b/tokio/src/runtime/scheduler/multi_thread/stats.rs index c59d4373ab8..262c3868f95 100644 --- a/tokio/src/runtime/scheduler/multi_thread/stats.rs +++ b/tokio/src/runtime/scheduler/multi_thread/stats.rs @@ -105,7 +105,10 @@ impl Stats { let mean_poll_duration = elapsed / num_polls; // Compute the alpha weighted by the number of tasks polled this batch. - let weighted_alpha = 1.0 - (1.0 - TASK_POLL_TIME_EWMA_ALPHA).powf(num_polls); + let mut weighted_alpha = TASK_POLL_TIME_EWMA_ALPHA * num_polls; + if weighted_alpha > 1.0 { + weighted_alpha = 1.0; + } // Now compute the new weighted average task poll time. self.task_poll_time_ewma = weighted_alpha * mean_poll_duration