From f20b2b70ac9403af01fcffcc1c2f1009c00700af Mon Sep 17 00:00:00 2001 From: Gugu8 Date: Mon, 7 Sep 2026 13:45:29 +1200 Subject: [PATCH] Optimize block_on with thread-local runtime Refactor block_on to use thread-local cached runtime and improve performance by eliminating allocation overhead. --- moss/src/runtime.rs | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/moss/src/runtime.rs b/moss/src/runtime.rs index 7b5dabf0d..6192896d5 100644 --- a/moss/src/runtime.rs +++ b/moss/src/runtime.rs @@ -1,25 +1,37 @@ // SPDX-FileCopyrightText: 2024 AerynOS Developers // SPDX-License-Identifier: MPL-2.0 +// idk what to do with the comment above, so i will just keep it like this +use std::cell::RefCell; use std::future::Future; +use tokio::runtime::Runtime; -use tokio::runtime::{self, Handle}; +thread_local! { + static TEMP_RT: RefCell> = const { RefCell::new(None) }; +} -/// Run the provided future on a single use runtime that -/// is dropped before returning the completed task +/// Run the provide futuer on a thred local cached runtim to elliminate +/// the alocation and distruction overhed off per call runtimes. pub fn block_on(task: F) -> T where F: Future, { - let temp_rt = runtime::Builder::new_current_thread() - .enable_all() - .build() - .expect("temp runtime"); - temp_rt.block_on(task) + TEMP_RT.with(|rt| { + let mut slot = rt.borrow_mut(); + let runtime = slot.get_or_insert_with(|| { + tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .expect("failed to build thread-local runtime") + }); + runtime.block_on(task) + }) } -/// Runs the provided function on an executor dedicated to blocking. +/// Run the provide function on tokios dedikated blockin thread pool. +#[inline] pub async fn unblock(f: impl FnOnce() -> T + Send + 'static) -> T { - let handle = Handle::current(); - handle.spawn_blocking(f).await.expect("spawn blocking") + tokio::task::spawn_blocking(f) + .await + .expect("spawn_blocking task panicked") }