Crates.io | vec_parallel |
lib.rs | vec_parallel |
version | 0.1.3 |
created_at | 2025-06-09 03:41:22.998206+00 |
updated_at | 2025-08-17 23:37:39.281291+00 |
description | Construct your Vec in parallel |
homepage | https://sealedabstract.com/code/vec_parallel |
repository | https://github.com/drewcrawford/vec_parallel |
max_upload_size | |
id | 1705479 |
size | 167,693 |
A library for building vectors in parallel using async tasks.
This crate provides an efficient, executor-agnostic way to construct Vec<T>
by dividing
the work into multiple async tasks that can run concurrently. It's particularly useful for
CPU-bound initialization tasks where elements can be computed independently.
vec_parallel allows you to parallelize the construction of vectors by splitting the work across multiple async tasks. Each task is responsible for computing a portion of the vector, writing directly to the final memory location to avoid unnecessary copies.
Strategy
]some_executor
feature for convenient spawninguse vec_parallel::{build_vec, Strategy};
// Build a vector of squares using multiple tasks
let builder = build_vec(100, Strategy::TasksPerCore(4), |i| i * i);
// Run the tasks (in a real application, these would be spawned on an executor)
for task in builder.tasks {
test_executors::spin_on(task);
}
// Get the final result
let squares = test_executors::spin_on(builder.result);
assert_eq!(squares[10], 100); // 10² = 100
use vec_parallel::{build_vec, Strategy};
// With tokio (or any async runtime)
let builder = build_vec(1000, Strategy::TasksPerCore(4), |i| {
// Expensive computation
(0..100).map(|j| (i + j) * 2).sum::<usize>()
});
// Spawn tasks on your executor
for task in builder.tasks {
// In a real app: tokio::spawn(task);
test_executors::spin_on(task);
}
// Await the result
let result = test_executors::spin_on(builder.result);
assert_eq!(result.len(), 1000);
The [Strategy
] enum controls how work is divided:
Strategy::One
]: No parallelism, single taskStrategy::Tasks(n)
]: Exactly n
tasksStrategy::Max
]: One task per element (maximum parallelism)Strategy::TasksPerCore(n)
]: n
tasks per CPU core (recommended for CPU-bound work)Strategy::TasksPerCore(4)
] to [Strategy::TasksPerCore(8)
]This library uses unsafe
code internally for performance, but maintains safety through:
Arc
and Weak
referencessome_executor
: Enables integration with the some_executor
crate for convenient task spawning