parallel
Parallel
Crystal library for parallel processing using Fiber::ExecutionContext.
Requirements
- Crystal 1.19.1+
- Compile flags:
-Dpreview_mt -Dexecution_context
Installation
Add to shard.yml:
dependencies:
parallel:
github: kojix2/parallel
Run shards install
Usage
require "parallel"
# Parallel map
[1, 2, 3, 4].par_map { |x| x * 2 }
# => [2, 4, 6, 8]
# Parallel each
[1, 2, 3, 4].par_each { |x| puts x }
# Chunk processing (fewer context switches)
[1, 2, 3, 4].par_map(chunk: 2) { |x| x * 2 }
# => [2, 4, 6, 8] (same result, fewer context switches)
# Custom ExecutionContext
context = Fiber::ExecutionContext::Parallel.new("workers", 8)
[1, 2, 3, 4].par_map(context) { |x| x * 2 }
By default, a dedicated Fiber::ExecutionContext::Parallel is created lazily. You may supply your own context if preferred.
# Use system default ExecutionContext
Parallel.execution_context = Fiber::ExecutionContext.default
[1, 2, 3, 4].par_map { |x| x * 2 }
Compilation
crystal build -Dpreview_mt -Dexecution_context your_app.cr
crystal spec -Dpreview_mt -Dexecution_context
Methods
Parallel (global)
-
execution_context : Fiber::ExecutionContext::Parallel
- Returns the library default context.
-
execution_context=(context : Fiber::ExecutionContext::Parallel)
- Sets the library default context.
Enumerable
-
par_map(execution_context = nil, *, chunk = nil, &block)
- Applies block to each element in parallel, returns array of results.
chunk: Process elements in chunks to reduce context switches.
-
par_each(execution_context = nil, *, chunk = nil, &block)
- Applies block to each element in parallel for side effects.
chunk: Process elements in chunks to reduce context switches.
Notes
- Works with any Enumerable (Array, Range, Set, Hash, etc.)
- Indexable types (Array, Slice) preserve order
- Exceptions are propagated from parallel tasks
- Uses a shared
Fiber::ExecutionContext::Parallelby default for performance (configurable viaParallel.execution_context) - Default worker count is based on
Fiber::ExecutionContext.default_workers_count(affected byCRYSTAL_WORKERSor CPU count) - Adaptive chunking clamps the computed chunk size between 1 and 1000
- Thread safety is your responsibility when accessing shared resources
Contributing
- Fork this repository
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
License
MIT
This library contains code generated by AI.
Repository
parallel
Owner
Statistic
- 1
- 0
- 0
- 0
- 0
- 21 days ago
- June 16, 2025
License
MIT License
Links
Synced at
Tue, 27 Jan 2026 14:14:50 GMT
Languages