Skip to main content
The parallel execution module provides utilities and macros for conditional parallelization. Code written with these macros automatically adapts to be parallel or serial based on feature flags.

Feature-Based Parallelism

The parallel module behavior is controlled by the serial feature flag:
When serial is NOT enabled:
  • Uses Rayon for parallel execution
  • Automatically utilizes multiple CPU cores
  • Best for production and performance-critical code
When serial IS enabled:
  • Falls back to standard sequential iterators
  • Single-threaded execution
  • Best for testing, debugging, and WebAssembly

Core Macros

cfg_iter!

Creates a parallel or serial iterator over references.
Expands to:

cfg_iter! with Minimum Length

Control the minimum chunk size for parallel execution:
This avoids parallelization overhead for small tasks.

cfg_iter_mut!

Creates a parallel or serial iterator over mutable references.

cfg_into_iter!

Creates a parallel or serial consuming iterator.

cfg_chunks!

Iterates over fixed-size chunks.

cfg_chunks_mut!

Iterates over mutable fixed-size chunks.

Collection Macros

cfg_keys!

Iterates over keys in a map.

cfg_values!

Iterates over values in a map.

Reduction Macros

cfg_reduce!

Applies a reduction operation.

cfg_reduce_with!

Reduces with a binary operation (no identity needed).
Returns Option because the collection might be empty.

Search Macros

cfg_find!

Finds an element matching a predicate.
Note: Returns at most one match, not necessarily the first in parallel mode.

cfg_find_map!

Finds and transforms an element.

Sorting Macros

cfg_sort_unstable_by!

Sorts a slice using an unstable sort.

cfg_sort_by_cached_key!

Sorts using a cached key function.

ExecutionPool

For dynamic job scheduling, use ExecutionPool:

With Capacity

Pre-allocate for better performance:

CPU Detection

The module detects CPU type to optimize thread usage.

max_available_threads()

Returns the optimal number of threads for the current CPU.
CPU-specific behavior: Intel CPUs:
  • Returns physical core count
  • Avoids hyperthreading overhead
  • Better for CPU-intensive cryptographic operations
AMD CPUs:
  • Returns all available threads
  • Leverages simultaneous multithreading (SMT)
  • Better overall throughput
Unknown CPUs:
  • Returns all available threads
  • Safe default

execute_with_max_available_threads()

Executes a closure with optimal thread count.
Automatically creates a thread pool if not already in one.

Common Patterns

Parallel Map

Parallel Filter

Parallel Filter-Map

Parallel Sum

Parallel Validation

Parallel Try-Map

Performance Guidelines

When to Use Parallelism

Good candidates for parallelization:
  • Large datasets (>1000 items)
  • CPU-intensive operations per item
  • Independent computations (no shared state)
  • Cryptographic operations (hashing, signatures, proofs)
Poor candidates for parallelization:
  • Small datasets (<100 items)
  • I/O-bound operations
  • Operations with heavy synchronization
  • Very fast operations (overhead dominates)

Example: Choosing Serial vs Parallel

Minimum Length Tuning

Use minimum length to avoid over-parallelization:

Testing with Serial Mode

For deterministic tests, enable serial mode:
This ensures:
  • Deterministic execution order
  • Easier debugging
  • No race conditions in tests

WebAssembly Support

For WebAssembly targets, always use serial mode:
WebAssembly has limited threading support, so serial execution is required.

Example: Parallel Proof Verification

Example: Parallel Batch Processing

Example: Dynamic Job Scheduling

Next Steps