You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2022/07/22 04:29:48 UTC

[groovy] branch master updated: GROOVY-10696: The DGM removeAll variants which take a closure can be refactored to use removeIf for better efficiency

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

paulk 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 a710d8960d GROOVY-10696: The DGM removeAll variants which take a closure can be refactored to use removeIf for better efficiency
a710d8960d is described below

commit a710d8960d5a971cf0fef7febc478216efc5d9c2
Author: Paul King <pa...@asert.com.au>
AuthorDate: Thu Jul 21 11:12:38 2022 +1000

    GROOVY-10696: The DGM removeAll variants which take a closure can be refactored to use removeIf for better efficiency
---
 .../groovy/runtime/DefaultGroovyMethods.java       | 26 ++--------
 .../callsite/BooleanClosureForMapPredicate.java    | 59 ++++++++++++++++++++++
 .../runtime/callsite/BooleanClosurePredicate.java  | 43 ++++++++++++++++
 3 files changed, 106 insertions(+), 22 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 54597fa465..0133358fa3 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -64,6 +64,8 @@ import org.codehaus.groovy.reflection.MixinInMetaClass;
 import org.codehaus.groovy.reflection.ReflectionCache;
 import org.codehaus.groovy.reflection.ReflectionUtils;
 import org.codehaus.groovy.reflection.stdclasses.CachedSAMClass;
+import org.codehaus.groovy.runtime.callsite.BooleanClosureForMapPredicate;
+import org.codehaus.groovy.runtime.callsite.BooleanClosurePredicate;
 import org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper;
 import org.codehaus.groovy.runtime.callsite.BooleanReturningMethodInvoker;
 import org.codehaus.groovy.runtime.dgmimpl.NumberNumberDiv;
@@ -4996,17 +4998,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
      * @since 1.7.2
      */
     public static <T> boolean removeAll(Collection<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure condition) {
-        Iterator iter = InvokerHelper.asIterator(self);
-        BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
-        boolean result = false;
-        while (iter.hasNext()) {
-            Object value = iter.next();
-            if (bcw.call(value)) {
-                iter.remove();
-                result = true;
-            }
-        }
-        return result;
+        return self.removeIf(new BooleanClosurePredicate<>(condition));
     }
 
     /**
@@ -5028,17 +5020,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
      * @since 2.5.0
      */
     public static <K, V> boolean removeAll(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure condition) {
-        Iterator<Map.Entry<K, V>> iter = self.entrySet().iterator();
-        BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
-        boolean result = false;
-        while (iter.hasNext()) {
-            Map.Entry<K, V> entry = iter.next();
-            if (bcw.callForMap(entry)) {
-                iter.remove();
-                result = true;
-            }
-        }
-        return result;
+        return self.entrySet().removeIf(new BooleanClosureForMapPredicate<>(condition));
     }
 
     /**
diff --git a/src/main/java/org/codehaus/groovy/runtime/callsite/BooleanClosureForMapPredicate.java b/src/main/java/org/codehaus/groovy/runtime/callsite/BooleanClosureForMapPredicate.java
new file mode 100644
index 0000000000..5d97a06b04
--- /dev/null
+++ b/src/main/java/org/codehaus/groovy/runtime/callsite/BooleanClosureForMapPredicate.java
@@ -0,0 +1,59 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.codehaus.groovy.runtime.callsite;
+
+import groovy.lang.Closure;
+
+import java.util.Map;
+import java.util.function.Predicate;
+
+/**
+ * Helper class for internal use only.
+ * This creates a Predicate by calling a {@link Closure} and converting the result to a boolean.
+ * {@link BooleanReturningMethodInvoker} is used for caching.
+ */
+public class BooleanClosureForMapPredicate<K, V> implements Predicate<Map.Entry<K, V>> {
+    private final BooleanReturningMethodInvoker bmi;
+    private final Closure wrapped;
+    private final int numberOfArguments;
+
+    public BooleanClosureForMapPredicate(Closure wrapped) {
+        this.wrapped = wrapped;
+        bmi = new BooleanReturningMethodInvoker("call");
+        numberOfArguments = wrapped.getMaximumNumberOfParameters();
+    }
+
+    private boolean call(Object... args) {
+        return bmi.invoke(wrapped, args);
+    }
+
+    /**
+     * If the call to the backing {@link Closure} is done on a {@link Closure}
+     * taking one argument, then we give in the {@link Map.Entry}, otherwise we will
+     * give in the key and value.
+     */
+    @Override
+    public boolean test(Map.Entry<K, V> entry) {
+        if (numberOfArguments == 2) {
+            return call(entry.getKey(), entry.getValue());
+        } else {
+            return call(entry);
+        }
+    }
+}
diff --git a/src/main/java/org/codehaus/groovy/runtime/callsite/BooleanClosurePredicate.java b/src/main/java/org/codehaus/groovy/runtime/callsite/BooleanClosurePredicate.java
new file mode 100644
index 0000000000..2ad3b3e9db
--- /dev/null
+++ b/src/main/java/org/codehaus/groovy/runtime/callsite/BooleanClosurePredicate.java
@@ -0,0 +1,43 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.codehaus.groovy.runtime.callsite;
+
+import groovy.lang.Closure;
+
+import java.util.function.Predicate;
+
+/**
+ * Helper class for internal use only.
+ * This creates a Predicate by calling a {@link Closure} and converting the result to a boolean.
+ * {@link BooleanReturningMethodInvoker} is used for caching.
+ */
+public class BooleanClosurePredicate<T> implements Predicate<T> {
+    private final BooleanReturningMethodInvoker bmi;
+    private final Closure wrapped;
+
+    public BooleanClosurePredicate(Closure wrapped) {
+        this.wrapped = wrapped;
+        this.bmi = new BooleanReturningMethodInvoker("call");
+    }
+
+    @Override
+    public boolean test(T arg) {
+        return bmi.invoke(wrapped, arg);
+    }
+}