[groovy] branch master updated: Tweak `groupBy` to support parallel better

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[groovy] branch master updated: Tweak `groupBy` to support parallel better

Daniel.Sun
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 39d2beb  Tweak `groupBy` to support parallel better
39d2beb is described below

commit 39d2beb2ffafdc133270804d06e3ca28d7cddc82
Author: Daniel Sun <[hidden email]>
AuthorDate: Sat Jan 9 21:25:43 2021 +0800

    Tweak `groupBy` to support parallel better
---
 .../ginq/provider/collection/runtime/QueryableCollection.java      | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
index 41eb603..534b714 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
@@ -49,6 +49,7 @@ import java.util.function.BiFunction;
 import java.util.function.BiPredicate;
 import java.util.function.Function;
 import java.util.function.Predicate;
+import java.util.stream.Collector;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import java.util.stream.StreamSupport;
@@ -219,9 +220,13 @@ class QueryableCollection<T> implements Queryable<T>, Serializable {
 
     @Override
     public Queryable<Tuple2<?, Queryable<T>>> groupBy(Function<? super T, ?> classifier, Predicate<? super Tuple2<?, Queryable<? extends T>>> having) {
+        Collector<T, ?, ? extends Map<?, List<T>>> groupingBy =
+                isParallel() ? Collectors.groupingByConcurrent(classifier, Collectors.toList())
+                             : Collectors.groupingBy(classifier, Collectors.toList());
+
         Stream<Tuple2<?, Queryable<T>>> stream =
                 this.stream()
-                        .collect(Collectors.groupingBy(classifier, Collectors.toList()))
+                        .collect(groupingBy)
                         .entrySet().stream()
                         .filter(m -> null == having || having.test(tuple(m.getKey(), from(m.getValue()))))
                         .map(m -> tuple(m.getKey(), from(m.getValue())));

Apache Groovy committer & PMC member

Blog: http://blog.sunlan.me
Twitter: @daniel_sun