You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2020/12/10 17:35:59 UTC

[groovy] branch master updated: Allocate reasonable space for buckets

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 6c979dc  Allocate reasonable space for buckets
6c979dc is described below

commit 6c979dc0f792135fe2e6fa5b5c25f38c0efda38f
Author: Daniel Sun <su...@apache.org>
AuthorDate: Fri Dec 11 01:34:43 2020 +0800

    Allocate reasonable space for buckets
---
 .../collection/runtime/QueryableCollection.java        | 18 +++++++++++-------
 .../spec/test/org/apache/groovy/ginq/GinqTest.groovy   | 12 ++++++++++++
 2 files changed, 23 insertions(+), 7 deletions(-)

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 42e25c4..0aebe6d 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
@@ -31,6 +31,7 @@ import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Comparator;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
@@ -100,12 +101,14 @@ class QueryableCollection<T> implements Queryable<T>, Serializable {
                 .collect(
                         Collectors.toMap(
                                 c -> hash(fieldsExtractor2.apply(c)),
-                                c -> {
-                                    List<U> list = new ArrayList<>();
-                                    list.add(c);
-                                    return list;
-                                },
+                                Collections::singletonList,
                                 (oldList, newList) -> {
+                                    if (!(oldList instanceof ArrayList)) {
+                                        List<U> tmpList = new ArrayList<>(HASHTABLE_BUCKET_INITIAL_SIZE);
+                                        tmpList.addAll(oldList);
+                                        oldList = tmpList;
+                                    }
+
                                     oldList.addAll(newList);
                                     return oldList;
                                 }
@@ -131,9 +134,10 @@ class QueryableCollection<T> implements Queryable<T>, Serializable {
         return from(stream);
     }
 
-    private static final int HASHTABLE_SIZE = SystemUtil.getIntegerSafe("groovy.ginq.hashtable.size", 128);
+    private static final int HASHTABLE_MAX_SIZE = SystemUtil.getIntegerSafe("groovy.ginq.hashtable.max.size", 128);
+    private static final int HASHTABLE_BUCKET_INITIAL_SIZE = SystemUtil.getIntegerSafe("groovy.ginq.hashtable.bucket.initial.size", 16);
     private static Integer hash(Object obj) {
-        return Objects.hash(obj) % HASHTABLE_SIZE; // mod 100 to limit the size of hash table
+        return Objects.hash(obj) % HASHTABLE_MAX_SIZE; // mod 100 to limit the size of hash table
     }
 
     @Override
diff --git a/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy b/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
index 246897e..277b4b1 100644
--- a/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
+++ b/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
@@ -3864,6 +3864,18 @@ class GinqTest {
     }
 
     @Test
+    void "testGinq - from innerhashjoin select - 5"() {
+        assertGinqScript '''
+            assert [[1, 1], [2, 2], [3, 3]] == GQ {
+                from n1 in 1..1000
+                innerhashjoin n2 in 1..10000 on n2 == n1
+                where n1 <= 3 && n2 <= 5
+                select n1, n2
+            }.toList()
+        '''
+    }
+
+    @Test
     void "testGinq - from innerhashjoin groupby select - 1"() {
         assertGinqScript '''
             import java.util.stream.Collectors