avila-parallel
A zero-dependency parallel computation library for Rust with true parallel execution.
๐ Documentation
- Quick Start - Get started in 5 minutes
- API Documentation - Full API reference
- Optimization Guide - Performance tuning tips
- Contributing - How to contribute
- Changelog - Version history
โจ Features
- ๐ True Parallel Execution: Real multi-threaded processing using
std::thread::scope - ๐ฆ Zero Dependencies: Only uses Rust standard library (
std::thread,std::sync) - ๐ Thread Safe: All operations use proper synchronization primitives
- ๐ Order Preservation: Results maintain original element order
- โก Smart Optimization: Automatically falls back to sequential for small datasets
- ๐ฏ Rich API: Familiar iterator-style methods
๐ Quick Start
Add to your Cargo.toml:
[]
= "0.1.0"
Basic Usage
use *;
๐ฏ Available Operations
Transformation
map- Transform each elementfilter- Keep elements matching predicatecloned- Clone elements (for reference iterators)
Aggregation
sum- Sum all elementsreduce- Reduce with custom operationfold- Fold with identity and operationcount- Count elements matching predicate
Search
find_any- Find any element matching predicateall- Check if all elements matchany- Check if any element matches
Other
partition- Split into two vectors based on predicatefor_each- Execute function on each elementcollect- Collect results into a collection
๐ Performance
The library automatically:
- Detects CPU core count
- Distributes work efficiently across threads
- Falls back to sequential execution for small datasets (< 512 elements/chunk)
- Maintains result order
Benchmark Results (10M elements)
| Operation | Sequential | Parallel | Speedup |
|---|---|---|---|
| Simple Sum | 7.1ms | 14.2ms | 0.50x* |
| Complex Computation | 229.5ms | 236.1ms | 0.97x |
| Filter | 67.5ms | 92.4ms | 0.73x |
*Simple operations have thread overhead. Use for CPU-bound work (>100ยตs per element).
๐ง Advanced Usage
Using Executor Functions Directly
use *;
let data = vec!;
// Parallel map
let results = parallel_map;
// Parallel filter
let evens = parallel_filter;
// Parallel reduce
let sum = parallel_reduce;
// Parallel partition
let = parallel_partition;
// Find first matching
let found = parallel_find;
// Count matching
let count = parallel_count;
Mutable Iteration
use *;
let mut data = vec!;
data.par_iter_mut
.for_each;
println!; // [2, 4, 6, 8, 10]
๐๏ธ Architecture
Thread Management
- Uses
std::thread::scopefor lifetime-safe thread spawning - Automatic CPU detection via
std::thread::available_parallelism() - Chunk-based work distribution with adaptive sizing
Synchronization
Arc<Mutex<>>for safe result collection- No unsafe code in public API
- Order preservation through indexed chunks
Performance Tuning
Default Configuration:
const MIN_CHUNK_SIZE: usize = 1024; // Optimized based on benchmarks
const MAX_CHUNKS_PER_THREAD: usize = 8;
Environment Variables:
# Customize minimum chunk size (useful for tuning specific workloads)
# Run your program
When to Adjust:
- Increase (2048+): Very expensive operations (>1ms per element)
- Decrease (512): Light operations but large datasets
- Keep default (1024): Most use cases
๐งช Examples
CPU-Intensive Computation
use *;
let data: = .collect;
// Perform expensive computation in parallel
let results = data.par_vec
.map
.collect;
Data Analysis
use *;
let data: = vec!;
// Calculate statistics in parallel
let sum: f64 = data.par_iter.sum;
let count = data.len;
let mean = sum / count as f64;
let variance = data.par_vec
.map
.into_iter
. / count as f64;
๐ When to Use
โ Good Use Cases
- CPU-bound operations (image processing, calculations, etc.)
- Large datasets (>10,000 elements)
- Independent computations per element
- Expensive operations (>100ยตs per element)
โ Not Ideal For
- I/O-bound operations (use async instead)
- Very small datasets (<1,000 elements)
- Simple operations (<10ยตs per element)
- Operations requiring shared mutable state
๐ ๏ธ Building from Source
๐ License
MIT License - see LICENSE file for details
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
๐ Documentation
Full API documentation is available at docs.rs/avila-parallel
๐ Related Projects
โญ Star History
If you find this project useful, consider giving it a star!