You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/06/09 14:24:47 UTC
[camel] branch main updated: (chores) core: list containsAll may perform poorly
This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new ce527ad5ce7 (chores) core: list containsAll may perform poorly
ce527ad5ce7 is described below
commit ce527ad5ce785dff87faac77a3c02c6a9e7da35f
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Fri Jun 9 15:25:04 2023 +0200
(chores) core: list containsAll may perform poorly
List contains all can have O(M*N) complexity and it can be faster to simply create a HashSet and use it instead
---
.../org/apache/camel/support/component/ApiMethodHelper.java | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/component/ApiMethodHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/component/ApiMethodHelper.java
index e20abf00b62..3b8f6717355 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/component/ApiMethodHelper.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/component/ApiMethodHelper.java
@@ -310,16 +310,18 @@ public final class ApiMethodHelper<T extends Enum<T> & ApiMethod> {
for (ApiMethod method : methods) {
final List<String> methodArgs = method.getArgNames();
+ final HashSet<String> stringHashSet = new HashSet<>(methodArgs);
+
switch (matchType) {
case EXACT:
// method must take all args, and no more
- if (methodArgs.containsAll(argNames) && argNames.containsAll(methodArgs)) {
+ if (stringHashSet.containsAll(argNames) && argNames.containsAll(methodArgs)) {
result.add(method);
}
break;
case SUBSET:
// all args are required, method may take more
- if (methodArgs.containsAll(argNames)) {
+ if (stringHashSet.containsAll(argNames)) {
result.add(method);
}
break;
@@ -327,7 +329,7 @@ public final class ApiMethodHelper<T extends Enum<T> & ApiMethod> {
case SUPER_SET:
// all method args must be present
if (argNames.containsAll(methodArgs)) {
- if (methodArgs.containsAll(argNames)) {
+ if (stringHashSet.containsAll(argNames)) {
// prefer exact match to avoid unused args
result.add(method);
} else if (result.isEmpty()) {
@@ -340,7 +342,7 @@ public final class ApiMethodHelper<T extends Enum<T> & ApiMethod> {
}
} else if (result.isEmpty() && extraArgs == null) {
// avoid looking for nullable args by checking for empty result and extraArgs
- if (withNullableArgsList != null && withNullableArgsList.containsAll(methodArgs)) {
+ if (withNullableArgsList != null && new HashSet<>(withNullableArgsList).containsAll(methodArgs)) {
if (nullArgs == null) {
nullArgs = new ArrayList<>();
}