Parallel collections (was Re: Exception transparency)

Nathan Bryant nathan.bryant at linkshare.com
Tue Jun 8 11:10:46 PDT 2010



Brian Goetz wrote:

> OK, I get the part about using the API to allow the programmer to
explicitly 
> choose between serial and parallel iteration.  And the views thing
lets us 
> decouple the choosing of serial/parallel from the actual point of
iteration (I 
> can pass an Iterator.par() and a closure to a method together, freeing
the 
> called code of guessing which to use.)

> What can we do to help programmers keep track of which closures are 
> "parallel-safe" and which not?

Obvious answer, pureness annotations. At the syntax level, not the
runtime level.

Sidestepping for the moment that the void closures likely to be passed
to forEach() seem likely to be inherently "side-effecty", views have
this problem:

/** base interface for Iterables with unspecified semantics: might be
sequential or parallel */
public interface Iterable<T> {
	void forEach(#void(T) block);
}

/** base interface for an Iterable that has parallel semantics. */
public interface ParallelIterable<T> extends Iterable<T> {
	void forEach(@pure #void(T) block);
}

Will ParallelIterable even compile? It doesn't seem to override
Iterable.forEach() according to the usual covariance semantics: it's
more specific. If it doesn't override, then it's a different method. If
it's a different method with seemingly the same parameter types,
reflective tools could get confused--an issue that it seems we must
accept if we go down this path-- because if the above problems seem to
leave us doing things this way:

/** base interface for Iterables with unspecified semantics: might be
sequential or parallel */
public interface Iterable<T> {
	void forEach(#void(T) block);
	void forEach(@pure #void(T) block);
}

/** tagging interface for Iterables with parallel semantics */
public interface ParallelIterable<T> extends Iterable<T> {
	/** @throws OperationNotSupportedException?? */
	void forEach(#void(T) block);
}

And when I want to coerce pureness, I've probably got to do something
like this, if call site annotations are not added:

@SuppressWarnings("pureness")
@pure
private void foo(T arg) {
...
}

... iter.par().forEach(#foo) // exact syntax TBD

... instead of being able to write the closure inline.


More information about the lambda-dev mailing list