Skip to content

Commit 9a897f2

Browse files
committed
Increased parallelism in Map/Reduce
1 parent cc32b6f commit 9a897f2

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

src/main/groovy/groovyx/gpars/GParsPoolUtil.groovy

+8-18
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ public class GParsPoolUtil {
716716
* This allows further parallel processing operations on the collection to chain and so effectively leverage the underlying
717717
* ParallelArray implementation.
718718
*/
719-
public static Object getParallel(Object collection) {
719+
public static PAWrapper getParallel(Object collection) {
720720
return getParallel(createCollection(collection))
721721
}
722722

@@ -761,7 +761,7 @@ abstract class AbstractPAWrapper<T> {
761761
* @return The product of reduction
762762
*/
763763
public final T reduce(Closure cl) {
764-
pa.reduce(cl as Reducer, null)
764+
pa.all().reduce(cl as Reducer, null)
765765
}
766766

767767
/**
@@ -844,26 +844,16 @@ abstract class AbstractPAWrapper<T> {
844844
* @param A closure indicating whether to propagate the given element into the filtered collection
845845
* @return A collection holding the allowed values
846846
*/
847-
public abstract AbstractPAWrapper filter(Closure cl)
847+
public AbstractPAWrapper filter(Closure cl) {
848+
new PAWrapper(pa.withFilter({cl(it)} as Predicate))
849+
}
848850
}
849851

850852
/**
851853
* The default ParallelArray wrapper class
852854
*/
853855
final class PAWrapper<T> extends AbstractPAWrapper {
854-
855-
def PAWrapper(final pa) {
856-
super(pa)
857-
}
858-
859-
/**
860-
* Filters concurrently elements in the collection based on the outcome of the supplied function on each of the elements.
861-
* @param A closure indicating whether to propagate the given element into the filtered collection
862-
* @return A collection holding the allowed values
863-
*/
864-
public PAWrapper filter(Closure cl) {
865-
new PAWrapper(pa.withFilter({cl(it)} as Predicate).all())
866-
}
856+
def PAWrapper(final pa) { super(pa) }
867857
}
868858

869859
/**
@@ -880,7 +870,7 @@ final class MappedPAWrapper<T> extends AbstractPAWrapper {
880870
* @param A closure indicating whether to propagate the given element into the filtered collection
881871
* @return A collection holding the allowed values
882872
*/
883-
public PAWrapper filter(Closure cl) {
884-
new PAWrapper(pa.all().withFilter({cl(it)} as Predicate).all())
873+
public final AbstractPAWrapper filter(Closure cl) {
874+
new PAWrapper(pa.all().withFilter({cl(it)} as Predicate))
885875
}
886876
}

src/test/groovy/groovyx/gpars/MapReduceTest.groovy

+12
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ public class MapReduceTest extends GroovyTestCase {
3535
}
3636
}
3737

38+
@SuppressWarnings("GroovyMethodWithMoreThanThreeNegations")
39+
public void testFilterOperations() {
40+
GParsPool.withPool(5) {
41+
assertEquals 'aa', 'abcde'.parallel.filter {it != 'e'}.map {it * 2}.filter {it != 'cc'}.min()
42+
assertEquals 'dd', 'abcde'.parallel.filter {it != 'e'}.map {it * 2}.filter {it != 'cc'}.max()
43+
assertEquals 'aabbdd', 'abcde'.parallel.filter {it != 'e'}.map {it * 2}.filter {it != 'cc'}.sum()
44+
assertEquals 3, 'abcde'.parallel.filter {it != 'e'}.map {it * 2}.filter {it != 'cc'}.size()
45+
assertEquals 4, 'abcde'.parallel.filter {it != 'e'}.map {it.size() * 2}.size()
46+
assertEquals 4, 'abcde'.parallel.filter {it != 'e'}.map {it.size() * 2}.collection.size()
47+
}
48+
}
49+
3850
public void testSeededReduce() {
3951
GParsPool.withPool(5) {
4052
assertEquals 15, [1, 2, 3, 4, 5].parallel.map {it}.reduce(0) {a, b -> a + b}

0 commit comments

Comments
 (0)