Skip to content

Commit 007cfed

Browse files
⚡ Bolt: [performance improvement] Optimize string replacement in TestFileNamePattern
Replaces regex matching with literal String.replace calls. This avoids regex compilation and matching overhead for literal replacements. Co-authored-by: RoiSoleil <3462260+RoiSoleil@users.noreply.github.com>
1 parent b0d94e7 commit 007cfed

7 files changed

Lines changed: 43 additions & 2 deletions

File tree

org.moreunit.core/src/org/moreunit/core/matching/TestFileNamePattern.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ private String buildPreferredTestFileName(String srcFileName)
532532

533533
if(! prefixPart.hasAlternatives() && ! suffixPart.hasAlternatives())
534534
{
535-
result = SRC_FILE_VARIABLE_PATTERN.matcher(patternString).replaceAll(quoteReplacement(srcFileName));
535+
result = patternString.replace(SRC_FILE_VARIABLE, srcFileName);
536536
}
537537
else if(! prefixPart.hasAlternatives())
538538
{
@@ -574,7 +574,7 @@ private List<String> buildPreferredTestFilePatterns(String quotedSrcFileName)
574574

575575
if(! prefixPart.hasAlternatives() && ! suffixPart.hasAlternatives())
576576
{
577-
result.add(SRC_FILE_VARIABLE_PATTERN.matcher(patternString).replaceAll(quoteReplacement(quotedSrcFileName)));
577+
result.add(patternString.replace(SRC_FILE_VARIABLE, quotedSrcFileName));
578578
}
579579
else if(! prefixPart.hasAlternatives())
580580
{

test_perf_3.class

1.35 KB
Binary file not shown.

test_perf_3.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class test_perf_3 {
2+
public static void main(String[] args) {
3+
String str = "\\QMyClassTest\\E.*";
4+
5+
java.util.regex.Pattern QUOTE_SEPARATORS_AND_WILDCARDS = java.util.regex.Pattern.compile("(?:\\\\Q|\\\\E|\\.\\*)");
6+
7+
System.out.println("Regex result: " + QUOTE_SEPARATORS_AND_WILDCARDS.matcher(str).replaceAll(""));
8+
System.out.println("Replace result: " + str.replace("\\Q", "").replace("\\E", "").replace(".*", ""));
9+
}
10+
}

test_perf_4.class

1.37 KB
Binary file not shown.

test_perf_4.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class test_perf_4 {
2+
public static void main(String[] args) {
3+
String patternString = "${srcFile}Test";
4+
String srcFileName = "TheWorld";
5+
6+
java.util.regex.Pattern SRC_FILE_VARIABLE_PATTERN = java.util.regex.Pattern.compile("\\$\\{srcFile\\}");
7+
8+
System.out.println("Regex result: " + SRC_FILE_VARIABLE_PATTERN.matcher(patternString).replaceAll(java.util.regex.Matcher.quoteReplacement(srcFileName)));
9+
System.out.println("Replace result: " + patternString.replace("${srcFile}", srcFileName));
10+
}
11+
}

test_perf_5.class

1.59 KB
Binary file not shown.

test_perf_5.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class test_perf_5 {
2+
public static void main(String[] args) {
3+
String patternString = "${srcFile}Test";
4+
String srcFileName = "TheWorld";
5+
6+
java.util.regex.Pattern SRC_FILE_VARIABLE_PATTERN = java.util.regex.Pattern.compile("\\$\\{srcFile\\}");
7+
8+
long start = System.nanoTime();
9+
for (int i = 0; i < 1000000; i++) {
10+
SRC_FILE_VARIABLE_PATTERN.matcher(patternString).replaceAll(java.util.regex.Matcher.quoteReplacement(srcFileName));
11+
}
12+
System.out.println("Regex: " + (System.nanoTime() - start) / 1000000 + "ms");
13+
14+
start = System.nanoTime();
15+
for (int i = 0; i < 1000000; i++) {
16+
patternString.replace("${srcFile}", srcFileName);
17+
}
18+
System.out.println("Replace: " + (System.nanoTime() - start) / 1000000 + "ms");
19+
}
20+
}

0 commit comments

Comments
 (0)