std/parallel
One thing going faster: Parallel, a pipeline whose stages run on the workers of the pool, the parallel() that
starts one from anything that can be iterated, and Cut, what a collection implements to be cut into chunks without
being copied first.
const squares = (0..1000).parallel().map({ _ * _ }).toList().await()
print squares.length()
The words are the ones of the sequential pipeline - map, filter, filterMap, collect, toList, count, sum,
minBy, maxBy, find, forEach - and a terminal answers a Task, because the work runs while the caller's worker
is free to do something else. Results arrive in input order, always: the input is cut into chunks whose borders depend
on its length alone (at most 64 of them), every chunk runs as a task an idle worker may take, and the chunks are read
back in order. So the answer never depends on the number of workers, the load of the machine or which chunk finished
first (docs/design/CONCURRENCY.md section 4).
Where the chunks come from. A Cut cuts itself: a List and an Array into lists of their items, a Range of
integers arithmetically into smaller ranges - (0..1000000).parallel() never builds a list of a million numbers.
Everything else that can be iterated - a Set, a Map, the characters of a String, a pipeline, a type of the
program's own - is read once into a list, which is then cut the same way. Either way the borders are the same function
of the length, so the answer is too.
One pass per chunk. The stages of a pipeline are fused: map followed by filter runs every chunk once, item by
item through both closures, and builds no list between them. What a chunk hands back is what the terminal keeps of
it - its items for toList, a number for count and sum, at most one item for find, minBy and maxBy.
Modules
std/parallel/libOne thing going faster: [Parallel], a pipeline whose stages run on the workers of the pool, theparallel()that starts one from anything that can be iterated, and [Cut], what a collection implements to be cut into chunks without being copied first.
Everything
- extend
Array<Item, Size> with Cut<Item, ArrayList<Item>>An array cuts into lists of its items, the way a list does. - trait
CutA collection that cuts itself into pieces without being read first: its length, and the piece of it the positions of a range cover. - extend
Cut<Item, Piece>Spreads a collection that cuts itself over the workers, without reading it first. - extend
Iterate<Item>Spreads a pipeline over the workers: anything that can be iterated. - extend
List<Item> with Cut<Item, ArrayList<Item>>A list cuts into lists of its items: a piece holds nothing the list holds, so a worker may take it as it is. - extend
List<Item>Spreads a list over the workers. - type
ParallelA pipeline whose stages run on several workers, with the results in input order. - extend
Range<Int> with Cut<Int, Range<Int>>A range of integers cuts into smaller ranges: positionindexis the valuestart + index.