Package par provides utilities for parallelizing computations.
Most implementations are built on parallelization via partitioning, i.e. data is divided into partitions, the partitions are mapped to intermediate representations in parallel, then the intermediate representations are combined (reduced) in parallel (where possible) to produce the desired result. This approach to parallelization provides a few key benefits:
- No synchronization required between threads during execution (coordination happens in the main thread).
- Number of allocations can be minimized as sizes become known before use.
- Sacrificing determinism is usually not necessary to get the maximum performance. In fact, in most cases deterministic implementations are the fastest option as they access memory in a linear fashion.
As with every performance-oriented tool, measure before applying. Most of the provided functionality is only beneficial if the datasets are large enough or the computations are expensive.
- func All[T any](values []T, predicate func(T) bool) bool
- func Any[T any](values []T, predicate func(T) bool) bool
- func Filter[T any](values []T, predicate func(T) bool) []T
- func Map[In, Out any](values []In, transform func(In) Out) []Out
- func None[T any](values []T, predicate func(T) bool) bool
- func Reduce[T any](values []T, accumulator func(T, T) T) T
This section is empty.
This section is empty.
All returns a boolean indicating if predicate returns true for all of the values.
A partition will terminate upon the first encountered value for which the predicate returns false, and as such, the predicate may not be called for every value.
Any returns a boolean indicating if predicate returns true for any of the values.
A partition will terminate upon the first encountered value for which the predicate returns true, and as such, the predicate may not be called for every value.
Filter returns a copy of the values slice without the values for which the predicate returns false.
The implementation is deterministic, and the returned slice maintains the order of the original values.
Internally, the implementation maps the values into per-partition bitmaps in parallel using the predicate, then creates a slice to store the results, then the bitmaps are used to map the values into the results slice in parallel.
func Map[In, Out any](values []In, transform func(In) Out) []Out
Map returns a slice of type Out by applying the transform function on every item in values.
The implementation is deterministic, and the returned slice maintains the order of the original values.
None returns a boolean indicating if predicate returns true for none of the values.
A partition will terminate upon the first encountered value for which the predicate returns true, and as such, the predicate may not be called for every value.
func Reduce[T any](values []T, accumulator func(T, T) T) T
Reduce reduces the values to a single one, by repeatedly applying an accumulator.
The accumulator is provided with two arguments:
- The result of the previous call to accumulator, OR, for the first call, the first element.
- The current element being processed.
The accumulator then returns the result of combining these two values.
The ordering of the accumulations is deterministic and linear only within a partition. The implementation works by reducing each partition into a single value and then reducing the values from each partition as they become ready.
Panics if values is an empty slice.
This section is empty.