Rust bindings for the KunQuant financial factor computation library.
KunQuant-rs provides safe Rust bindings for KunQuant, a high-performance financial factor computation library. This crate wraps the C API with safe Rust abstractions while maintaining the performance characteristics of the underlying library.
- Safe Rust API: All unsafe FFI calls are wrapped in safe abstractions
- RAII Resource Management: Automatic cleanup of resources using Rust's Drop trait
- Batch Computation: Efficient batch processing of financial factors
- Multi-threading Support: Both single-thread and multi-thread executors
- Streaming Computation: Real-time factor calculation with low latency
- Memory Safety: Proper lifetime management and buffer handling
This project depends on the KunRuntime library. Follow these steps to ensure proper linking:
-
Compile with explicit library path:
cargo rustc -- -L /path/to/KunQuant/runner
Replace
/path/to/KunQuant/runnerwith the actual path tolibKunRuntime.so. -
Permanent configuration via
RUSTFLAGS:export RUSTFLAGS="-L /path/to/KunQuant/runner" cargo build
-
Runtime configuration: Ensure the library is available at runtime by setting
LD_LIBRARY_PATH:export LD_LIBRARY_PATH=/path/to/KunQuant/runner:$LD_LIBRARY_PATH
use kunquant_rs::{Executor, Library, BufferNameMap, BatchParams, run_graph};
// Create executor and load library
let executor = Executor::single_thread()?;
let library = Library::load("test_libs/simple_test_lib.so")?;
let module = library.get_module("simple_test")?;
// Prepare data
let mut input_data = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
let mut output_data = vec![0.0; 8];
// Set up buffers
let mut buffers = BufferNameMap::new()?;
buffers.set_buffer_slice("input", &mut input_data)?;
buffers.set_buffer_slice("output", &mut output_data)?;
// Run computation
let params = BatchParams::full_range(8, 1)?;
run_graph(&executor, &module, &buffers, ¶ms)?;
// Results are now in output_data
println!("Results: {:?}", output_data);Executor: Manages computation execution (single-thread or multi-thread)Library: Represents a loaded factor libraryModule: A specific factor module within a libraryBufferNameMap: Maps buffer names to data slicesBatchParams: Parameters for batch computationStreamContext: Context for streaming computation
run_graph(): Execute a factor computation graphExecutor::single_thread(): Create single-thread executorExecutor::multi_thread(n): Create multi-thread executor with n threadsLibrary::load(path): Load a factor library from file
Run tests with the provided script that sets up the correct library path:
./run_tests.shThe library follows a layered architecture:
- FFI Layer (
ffi.rs): Raw C bindings - Error Handling (
error.rs): Rust error types - Core Types (
executor.rs,library.rs): Safe wrappers - Buffer Management (
buffer.rs): Memory-safe buffer handling - Computation APIs (
batch.rs,stream.rs): High-level computation interfaces
- All C resources are automatically cleaned up using Rust's RAII pattern
- Buffer lifetimes are tracked to prevent use-after-free
- No manual memory management required
- Zero-cost abstractions over the C API
- Efficient buffer management with minimal copying
- Multi-threading support for parallel computation
- Requires KunQuant C library to be installed and accessible
- Factor libraries must be pre-compiled using the Python interface
- Streaming factors require special compilation with
output_layout="STREAM"
- Ensure all tests pass:
./run_tests.sh - Add tests for new functionality
- Follow Rust naming conventions and safety guidelines
- Document public APIs
MIT License - see LICENSE file for details.