Skip to content

Commit c8000fc

Browse files
committed
Cache generated {arguments} patterns for parameterized tests
Prior to this commit, {arguments} patterns were generated for every @⁠ParameterizedTest and @⁠ParameterizedClass, which could result in generation of the same pattern strings hundreds of times per test suite. Specifically, without caching, JUnit Jupiter generated strings like "{0}, {1}", "{0}, {1}, {2}", "{0}, {1}, {2}, {3}", etc. each time such a pattern was needed. To prevent regeneration of identical patterns, this commit modifies ParameterizedInvocationNameFormatter so that it caches each generated pattern in a map, keyed by the number of arguments. Prior to this commit, we generated {arguments} patterns 330 times when running JupiterTestSuite. Whereas, with caching in place we now only generate {arguments} patterns 10 times for the same test suite. Closes #4804
1 parent b9618a8 commit c8000fc

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedInvocationNameFormatter.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
package org.junit.jupiter.params;
1212

1313
import static java.util.Objects.requireNonNull;
14-
import static java.util.stream.Collectors.joining;
1514
import static org.junit.jupiter.params.ParameterizedInvocationConstants.ARGUMENTS_PLACEHOLDER;
1615
import static org.junit.jupiter.params.ParameterizedInvocationConstants.ARGUMENTS_WITH_NAMES_PLACEHOLDER;
1716
import static org.junit.jupiter.params.ParameterizedInvocationConstants.ARGUMENT_SET_NAME_OR_ARGUMENTS_WITH_NAMES_PLACEHOLDER;
@@ -30,10 +29,10 @@
3029
import java.util.Map;
3130
import java.util.Optional;
3231
import java.util.Set;
32+
import java.util.StringJoiner;
3333
import java.util.concurrent.ConcurrentHashMap;
3434
import java.util.concurrent.ConcurrentMap;
3535
import java.util.function.Function;
36-
import java.util.stream.IntStream;
3736

3837
import org.jspecify.annotations.Nullable;
3938
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
@@ -48,6 +47,12 @@
4847
*/
4948
class ParameterizedInvocationNameFormatter {
5049

50+
/**
51+
* Global cache for {arguments} pattern strings, keyed by the number of arguments.
52+
* @since 6.0
53+
*/
54+
private static final Map<Integer, String> argumentsPatternCache = new ConcurrentHashMap<>(8);
55+
5156
static final String DEFAULT_DISPLAY_NAME = "{default_display_name}";
5257
static final String DEFAULT_DISPLAY_NAME_PATTERN = "[" + INDEX_PLACEHOLDER + "] "
5358
+ ARGUMENT_SET_NAME_OR_ARGUMENTS_WITH_NAMES_PLACEHOLDER;
@@ -186,9 +191,14 @@ private PartialFormatters createPartialFormatters(String displayName,
186191
}
187192

188193
private static String argumentsPattern(int length) {
189-
return IntStream.range(0, length) //
190-
.mapToObj(index -> "{" + index + "}") //
191-
.collect(joining(", "));
194+
return argumentsPatternCache.computeIfAbsent(length, //
195+
key -> {
196+
StringJoiner sj = new StringJoiner(", ");
197+
for (int i = 0; i < length; i++) {
198+
sj.add("{" + i + "}");
199+
}
200+
return sj.toString();
201+
});
192202
}
193203

194204
private record PlaceholderPosition(int index, String placeholder) {

0 commit comments

Comments
 (0)