This is an automated email from the ASF dual-hosted git repository.
markt pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push:
new 5b30d52b51 Fix BZ 69381 Improve method lookup performance
5b30d52b51 is described below
commit 5b30d52b5140a9472a19e8c214f98e27b30ad0f5
Author: Mark Thomas <[email protected]>
AuthorDate: Thu Oct 31 20:31:28 2024 +0000
Fix BZ 69381 Improve method lookup performance
When the method has no arguments there is no requirement to consider
casting or coercion. Shortcut the method lookup process in that case.
Based on PR #770 by John Engebretson.
---
java/jakarta/el/Util.java | 10 ++
.../apache/el/parser/TestELParserPerformance.java | 121 +++++++++++++++++----
test/org/apache/el/parser/TesterBeanD.java | 73 +++++++++++++
test/org/apache/el/parser/TesterBeanE.java | 70 ++++++++++++
test/org/apache/el/parser/TesterEnum.java | 23 ++++
5 files changed, 273 insertions(+), 24 deletions(-)
diff --git a/java/jakarta/el/Util.java b/java/jakarta/el/Util.java
index c168de6e5b..59bc98b538 100644
--- a/java/jakarta/el/Util.java
+++ b/java/jakarta/el/Util.java
@@ -202,6 +202,16 @@ class Util {
paramTypes = getTypesFromValues(paramValues);
}
+ // Fast path: when no arguments exist, there can only be one matching
method and no need for coercion.
+ if (paramTypes.length == 0) {
+ try {
+ Method method = clazz.getMethod(methodName, paramTypes);
+ return getMethod(clazz, base, method);
+ } catch (NoSuchMethodException | SecurityException e) {
+ // Fall through to broader, slower logic
+ }
+ }
+
Method[] methods = clazz.getMethods();
List<Wrapper<Method>> wrappers = Wrapper.wrap(methods, methodName);
diff --git a/test/org/apache/el/parser/TestELParserPerformance.java
b/test/org/apache/el/parser/TestELParserPerformance.java
index c5e8b5a3bc..85eac64240 100644
--- a/test/org/apache/el/parser/TestELParserPerformance.java
+++ b/test/org/apache/el/parser/TestELParserPerformance.java
@@ -17,6 +17,10 @@
package org.apache.el.parser;
import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.concurrent.Callable;
import jakarta.el.ELBaseTest;
import jakarta.el.ELContext;
@@ -35,20 +39,16 @@ import org.apache.tomcat.util.collections.SynchronizedStack;
*/
public class TestELParserPerformance extends ELBaseTest {
+ private static final long DEFAULT_TEST_ITERATIONS = 1000000;
+
/*
* Test to explore if re-using Parser instances is faster.
*
- * Tests on my laptop show:
- * - overhead by introducing the stack is in the noise for parsing even the
- * simplest expression
- * - efficiency from re-using the ELParser is measurable for even a single
- * reuse of the parser
- * - with large numbers of parses (~10k) performance for a trivial parse is
- * three times faster
- * - around the 100 iterations mark GC overhead adds significant noise to
- * the results - for consistent results you either need fewer parses to
- * avoid triggering GC or more parses so the GC effects are evenly
- * distributed between the runs
+ * Tests on my laptop show: - overhead by introducing the stack is in the
noise for parsing even the simplest
+ * expression - efficiency from re-using the ELParser is measurable for
even a single reuse of the parser - with
+ * large numbers of parses (~10k) performance for a trivial parse is three
times faster - around the 100 iterations
+ * mark GC overhead adds significant noise to the results - for consistent
results you either need fewer parses to
+ * avoid triggering GC or more parses so the GC effects are evenly
distributed between the runs
*
* Note that the test is single threaded.
*/
@@ -60,11 +60,11 @@ public class TestELParserPerformance extends ELBaseTest {
long reinitTotalTime = 0;
long newTotalTime = 0;
- for (int j = 0; j < runs; j ++) {
+ for (int j = 0; j < runs; j++) {
long start = System.nanoTime();
SynchronizedStack<ELParser> stack = new SynchronizedStack<>();
- for (int i = 0; i < parseIterations; i ++) {
+ for (int i = 0; i < parseIterations; i++) {
ELParser parser = stack.pop();
if (parser == null) {
parser = new ELParser(new StringReader("${'foo'}"));
@@ -75,23 +75,21 @@ public class TestELParserPerformance extends ELBaseTest {
stack.push(parser);
}
long end = System.nanoTime();
- reinitTotalTime += (end - start);
+ reinitTotalTime += (end - start);
- System.out.println(parseIterations +
- " iterations using ELParser.ReInit(...) took " + (end -
start) + "ns");
+ System.out.println(parseIterations + " iterations using
ELParser.ReInit(...) took " + (end - start) + "ns");
}
- for (int j = 0; j < runs; j ++) {
+ for (int j = 0; j < runs; j++) {
long start = System.nanoTime();
- for (int i = 0; i < parseIterations; i ++) {
+ for (int i = 0; i < parseIterations; i++) {
ELParser parser = new ELParser(new StringReader("${'foo'}"));
parser.CompositeExpression();
}
long end = System.nanoTime();
- newTotalTime += (end - start);
+ newTotalTime += (end - start);
- System.out.println(parseIterations +
- " iterations using new ELParser(...) took " + (end -
start) + "ns");
+ System.out.println(parseIterations + " iterations using new
ELParser(...) took " + (end - start) + "ns");
}
Assert.assertTrue("Using new ElParser() was faster then using
ELParser.ReInit", reinitTotalTime < newTotalTime);
@@ -112,7 +110,7 @@ public class TestELParserPerformance extends ELBaseTest {
long durations[] = new long[9];
for (int j = 0; j < 5; j++) {
- for (int operandCount = 2; operandCount < 11; operandCount ++) {
+ for (int operandCount = 2; operandCount < 11; operandCount++) {
StringBuilder sb = new StringBuilder("${true");
for (int i = 2; i <= operandCount; i++) {
@@ -137,8 +135,8 @@ public class TestELParserPerformance extends ELBaseTest {
}
}
}
- for (int operandCount = 2; operandCount < 11; operandCount ++) {
- System.out.println("Operand count [" + operandCount + "], duration
[" + durations[operandCount -2] + "]");
+ for (int operandCount = 2; operandCount < 11; operandCount++) {
+ System.out.println("Operand count [" + operandCount + "], duration
[" + durations[operandCount - 2] + "]");
}
System.out.println("");
}
@@ -174,4 +172,79 @@ public class TestELParserPerformance extends ELBaseTest {
}
System.out.println("");
}
+
+
+ /*
+ * Utility method for running tasks provided by the test*() methods.
+ *
+ * @param c Callable to be executed.
+ * @param description Label to be displayed in the results.
+ *
+ * @return Arbitrary response data provided by the Callable.
+ */
+ private Object runTest(Callable<Object> c, String description) throws
Exception {
+
+ Object result = null;
+ for (int j = 0; j < 5; j++) {
+
+ System.gc();
+ long start = System.currentTimeMillis();
+
+ for (int i = 0; i < DEFAULT_TEST_ITERATIONS; i++) {
+ result = c.call();
+ }
+
+ long duration = System.currentTimeMillis() - start;
+ System.out.println(description + " duration=[" + duration + "ms]
or " +
+ (duration * 1000000 / DEFAULT_TEST_ITERATIONS + " ns
each"));
+
+ }
+ System.out.println("Result: " + result);
+
+ return result;
+ }
+
+
+ /*
+ * Performance test of various expressions. This can be easily modified to
explore the performance of other
+ * expressions.
+ *
+ * The operations tested in this method allocate many objects. For most
consistent results, use runtime arguments
+ * such as these: -Xms3g -Xmx3g -XX:+UseParallelGC
+ *
+ * Ignored by default since this is an absolute test primarily for
+ * https://bz.apache.org/bugzilla/show_bug.cgi?id=69381
+ */
+ @Ignore
+ @Test
+ public void testExpressions() throws Exception {
+
+ ELManager manager = new ELManager();
+ ELContext context = manager.getELContext();
+ ExpressionFactory factory = ELManager.getExpressionFactory();
+ TesterBeanD beanD = new TesterBeanD();
+ TesterBeanE beanE = new TesterBeanE();
+ beanD.setBean(beanE);
+ beanD.setName("name");
+ beanE.setName("name");
+ beanD.setStringMap(new HashMap<>());
+ beanD.setValList(new ArrayList<>(Arrays.asList("a", "b", "c")));
+
+ ValueExpression var = factory.createValueExpression(beanD,
TesterBeanD.class);
+ context.getVariableMapper().setVariable("beanD", var);
+
+ ValueExpression varB = factory.createValueExpression(beanE,
TesterBeanE.class);
+ context.getVariableMapper().setVariable("beanE", varB);
+
+ var = factory.createValueExpression(TesterEnum.ONE, TesterEnum.class);
+ context.getVariableMapper().setVariable("enumOne", var);
+
+ String[] expressions = new String[] { "${beanD.name}",
"${beanD.getName()}", "${beanE.getName()}" };
+ for (String expression : expressions) {
+ runTest(() -> {
+ ValueExpression ve = factory.createValueExpression(context,
expression, Object.class);
+ return ve.getValue(context);
+ }, expression);
+ }
+ }
}
diff --git a/test/org/apache/el/parser/TesterBeanD.java
b/test/org/apache/el/parser/TesterBeanD.java
new file mode 100644
index 0000000000..f85c32f761
--- /dev/null
+++ b/test/org/apache/el/parser/TesterBeanD.java
@@ -0,0 +1,73 @@
+/*
+ * 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.apache.el.parser;
+
+import java.util.List;
+import java.util.Map;
+
+/*
+ * Bean class used to evaluate EL expressions with two or more complex types.
+ * Provides 10 methods.
+ */
+public class TesterBeanD {
+
+ private TesterBeanE bean;
+ private String name;
+ private Map<String,String> stringMap;
+ private List<?> valList;
+ private long valLong;
+
+ public TesterBeanE getBean() {
+ return bean;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public Map<String,String> getStringMap() {
+ return stringMap;
+ }
+
+ public List<?> getValList() {
+ return valList;
+ }
+
+ public long getValLong() {
+ return valLong;
+ }
+
+ public void setBean(TesterBeanE bean) {
+ this.bean = bean;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public void setStringMap(Map<String,String> stringMap) {
+ this.stringMap = stringMap;
+ }
+
+ public void setValList(List<?> valList) {
+ this.valList = valList;
+ }
+
+ public void setValLong(long valLong) {
+ this.valLong = valLong;
+ }
+}
diff --git a/test/org/apache/el/parser/TesterBeanE.java
b/test/org/apache/el/parser/TesterBeanE.java
new file mode 100644
index 0000000000..ff45992c7b
--- /dev/null
+++ b/test/org/apache/el/parser/TesterBeanE.java
@@ -0,0 +1,70 @@
+/*
+ * 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.apache.el.parser;
+
+/*
+ * Bean class used to evaluate EL expressions with two or more complex types.
Provides 10 methods, plus those on the
+ * superclass.
+ */
+public class TesterBeanE extends TesterBeanD {
+
+ private String four;
+ private String one;
+ private String three;
+ private String two;
+ private String five;
+
+ public String getFive() {
+ return five;
+ }
+
+ public void setFive(String five) {
+ this.five = five;
+ }
+
+ public String getFour() {
+ return four;
+ }
+
+ public String getOne() {
+ return one;
+ }
+
+ public String getThree() {
+ return three;
+ }
+
+ public String getTwo() {
+ return two;
+ }
+
+ public void setFour(String four) {
+ this.four = four;
+ }
+
+ public void setOne(String one) {
+ this.one = one;
+ }
+
+ public void setThree(String three) {
+ this.three = three;
+ }
+
+ public void setTwo(String two) {
+ this.two = two;
+ }
+}
diff --git a/test/org/apache/el/parser/TesterEnum.java
b/test/org/apache/el/parser/TesterEnum.java
new file mode 100644
index 0000000000..3d6be2104c
--- /dev/null
+++ b/test/org/apache/el/parser/TesterEnum.java
@@ -0,0 +1,23 @@
+/*
+ * 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.apache.el.parser;
+
+public enum TesterEnum {
+ ONE,
+ TWO,
+ THREE
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]