Skip to content

Commit 5e4632e

Browse files
committed
Optimize MethodKey initialization and hashCode
Let's stay away from Objects.hash() as it allocates a new array every time to pass the parameters. When the map is resized quite a lot, it can start causing issues.
1 parent c6a6fb4 commit 5e4632e

File tree

1 file changed

+24
-5
lines changed
  • independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor

1 file changed

+24
-5
lines changed

independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/Methods.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,27 @@ static class MethodKey {
337337
final List<DotName> params;
338338
final DotName returnType;
339339
final MethodInfo method; // this is intentionally ignored for equals/hashCode
340+
private final int hashCode;
340341

341342
public MethodKey(MethodInfo method) {
342343
this.method = Objects.requireNonNull(method, "Method must not be null");
343344
this.name = method.name();
344345
this.returnType = method.returnType().name();
345-
this.params = new ArrayList<>();
346-
for (Type i : method.parameterTypes()) {
347-
params.add(i.name());
348-
}
346+
this.params = switch (method.parametersCount()) {
347+
case 0 -> List.of();
348+
case 1 -> List.of(method.parameterTypes().get(0).name());
349+
case 2 -> List.of(method.parameterTypes().get(0).name(), method.parameterTypes().get(1).name());
350+
default -> {
351+
List<DotName> ret = new ArrayList<>(method.parametersCount());
352+
for (Type parameterType : method.parameterTypes()) {
353+
ret.add(parameterType.name());
354+
}
355+
yield ret;
356+
}
357+
};
358+
359+
// the Map can be resized several times so it's worth caching the hashCode
360+
this.hashCode = buildHashCode(this.name, this.params, this.returnType);
349361
}
350362

351363
@Override
@@ -362,7 +374,14 @@ public boolean equals(Object o) {
362374

363375
@Override
364376
public int hashCode() {
365-
return Objects.hash(name, params, returnType);
377+
return hashCode;
378+
}
379+
380+
private static int buildHashCode(String name, List<DotName> params, DotName returnType) {
381+
int result = Objects.hashCode(name);
382+
result = 31 * result + Objects.hashCode(params);
383+
result = 31 * result + Objects.hashCode(returnType);
384+
return result;
366385
}
367386
}
368387

0 commit comments

Comments
 (0)