Skip to main content

Executor

Struct Executor 

Source
pub struct Executor { /* private fields */ }
Expand description

A KunQuant executor responsible for running factor computations.

The Executor manages the computational resources and threading model used for factor calculations. It provides both single-threaded and multi-threaded execution modes to optimize performance based on workload characteristics.

§Thread Safety

  • The executor itself is thread-safe and can be shared across threads
  • Multiple computations can be executed concurrently using the same executor
  • Each computation maintains its own state and memory buffers

§Memory Management

The executor automatically manages its resources using RAII. The underlying C handle is properly cleaned up when the executor is dropped.

§Performance Considerations

  • Single-threaded executors have lower overhead for small computations
  • Multi-threaded executors scale better for large factor libraries
  • Thread count should typically match CPU core count for optimal performance

Implementations§

Source§

impl Executor

Source

pub fn single_thread() -> Result<Self>

Creates a single-threaded executor optimized for low-latency computations.

Single-threaded executors are ideal for:

  • Real-time streaming applications where latency is critical
  • Simple factors with low computational complexity
  • Scenarios where thread synchronization overhead outweighs benefits
  • Development and testing environments
§Returns

Returns Ok(Executor) on success, or Err(KunQuantError::ExecutorCreationFailed) if the underlying C library fails to create the executor.

§Examples
use kunquant_rs::Executor;

// Create a single-threaded executor for low-latency processing
let executor = Executor::single_thread()?;

// Use with streaming context for real-time factor calculation
// let stream = StreamContext::new(&executor, &module, 16)?;
§Performance Notes
  • Minimal thread synchronization overhead
  • Predictable execution timing
  • Lower memory footprint compared to multi-threaded executors
  • Best suited for factors processing fewer than 1000 stocks
Source

pub fn multi_thread(num_threads: i32) -> Result<Self>

Creates a multi-threaded executor for high-throughput batch computations.

Multi-threaded executors are ideal for:

  • Large-scale batch processing of historical data
  • Complex factors with high computational requirements
  • Processing thousands of stocks simultaneously
  • Scenarios where throughput is more important than latency
§Arguments
  • num_threads - Number of worker threads to create. Should typically match the number of CPU cores for optimal performance. Values less than 1 will result in creation failure.
§Returns

Returns Ok(Executor) on success, or Err(KunQuantError::ExecutorCreationFailed) if:

  • num_threads is less than 1
  • The underlying C library fails to create the executor
  • System resources are insufficient for the requested thread count
§Examples
use kunquant_rs::Executor;

// Create executor with 4 worker threads
let executor = Executor::multi_thread(4)?;

// Optimal for CPU-bound batch processing
let cpu_cores = std::thread::available_parallelism().unwrap().get() as i32;
let optimal_executor = Executor::multi_thread(cpu_cores)?;
§Performance Notes
  • Scales well with CPU core count for compute-intensive factors
  • Higher memory usage due to per-thread buffers
  • Thread synchronization adds latency overhead
  • Best suited for batch processing of large datasets
  • Diminishing returns beyond CPU core count due to memory bandwidth limits

Trait Implementations§

Source§

impl Drop for Executor

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Executor

Source§

impl Sync for Executor

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.