KunQuant-rs
Rust bindings for the KunQuant financial factor computation library.
Overview
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.
Features
- 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
Quick Start
Dependencies
This project depends on the KunRuntime library. Follow these steps to ensure proper linking:
Linking libKunRuntime.so
-
Compile with explicit library path:
Replace
/path/to/KunQuant/runnerwith the actual path tolibKunRuntime.so. -
Permanent configuration via
RUSTFLAGS: -
Runtime configuration: Ensure the library is available at runtime by setting
LD_LIBRARY_PATH:
Basic Usage
use ;
// Create executor and load library
let executor = single_thread?;
let library = load?;
let module = library.get_module?;
// Prepare data
let mut input_data = vec!;
let mut output_data = vec!;
// Set up buffers
let mut buffers = new?;
buffers.set_buffer_slice?;
buffers.set_buffer_slice?;
// Run computation
let params = full_range?;
run_graph?;
// Results are now in output_data
println!;
API Documentation
Core Types
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
Key Functions
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
Testing
Run tests with the provided script that sets up the correct library path:
Architecture
The 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
Memory Management
- 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
Performance
- Zero-cost abstractions over the C API
- Efficient buffer management with minimal copying
- Multi-threading support for parallel computation
- Stock count must be a multiple of 8 for optimal SIMD performance
Limitations
- 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"
Contributing
- Ensure all tests pass:
./run_tests.sh - Add tests for new functionality
- Follow Rust naming conventions and safety guidelines
- Document public APIs
License
MIT License - see LICENSE file for details.