Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 6 additions & 28 deletions crates/engine/tree/src/tree/payload_processor/executor.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,27 @@
//! Executor for mixed I/O and CPU workloads.
use rayon::ThreadPool as RayonPool;
use std::{
sync::{Arc, OnceLock},
time::Duration,
};
use std::{sync::OnceLock, time::Duration};
use tokio::{
runtime::{Builder, Handle, Runtime},
task::JoinHandle,
};

/// An executor for mixed I/O and CPU workloads.
///
/// This type has access to its own rayon pool and uses tokio to spawn blocking tasks.
///
/// It will reuse an existing tokio runtime if available or create its own.
/// This type uses tokio to spawn blocking tasks and will reuse an existing tokio
/// runtime if available or create its own.
#[derive(Debug, Clone)]
pub struct WorkloadExecutor {
inner: WorkloadExecutorInner,
}

impl Default for WorkloadExecutor {
fn default() -> Self {
Self { inner: WorkloadExecutorInner::new(rayon::ThreadPoolBuilder::new().build().unwrap()) }
Self { inner: WorkloadExecutorInner::new() }
}
}

impl WorkloadExecutor {
/// Creates a new executor with the given number of threads for cpu bound work (rayon).
#[expect(unused)]
pub(super) fn with_num_cpu_threads(cpu_threads: usize) -> Self {
Self {
inner: WorkloadExecutorInner::new(
rayon::ThreadPoolBuilder::new().num_threads(cpu_threads).build().unwrap(),
),
}
}

/// Returns the handle to the tokio runtime
pub(super) const fn handle(&self) -> &Handle {
&self.inner.handle
Expand All @@ -51,22 +36,15 @@ impl WorkloadExecutor {
{
self.inner.handle.spawn_blocking(func)
}

/// Returns access to the rayon pool
#[expect(unused)]
pub(super) const fn rayon_pool(&self) -> &Arc<rayon::ThreadPool> {
&self.inner.rayon_pool
}
}

#[derive(Debug, Clone)]
struct WorkloadExecutorInner {
handle: Handle,
rayon_pool: Arc<RayonPool>,
}

impl WorkloadExecutorInner {
fn new(rayon_pool: rayon::ThreadPool) -> Self {
fn new() -> Self {
fn get_runtime_handle() -> Handle {
Handle::try_current().unwrap_or_else(|_| {
// Create a new runtime if no runtime is available
Expand All @@ -90,6 +68,6 @@ impl WorkloadExecutorInner {
})
}

Self { handle: get_runtime_handle(), rayon_pool: Arc::new(rayon_pool) }
Self { handle: get_runtime_handle() }
}
}
Loading