Skip to content

Commit 6210c73

Browse files
Optimize String concatenation in TestFolderPathPattern loops
This commit replaces inefficient string concatenations and `replaceFirst` inside loops with optimized `StringBuilder` and `Matcher.appendReplacement` strategies. This addresses performance bottlenecks when repeatedly updating `result` within `resolveGroups` and parsing large template strings in `replaceGroupsWithRefs`. Co-authored-by: RoiSoleil <3462260+RoiSoleil@users.noreply.github.com>
1 parent f72909a commit 6210c73

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public class TestFolderPathPattern
4848
TEST_PATH_VALIDATOR = compile("^/?[^/\\*\\(\\)]*" + quote(SRC_PROJECT_VARIABLE) + "[^\\*\\(\\)]*$");
4949
}
5050

51+
private static final Pattern GROUP_PATTERN = Pattern.compile("\\([^\\)]+\\)");
52+
5153
private final String srcPathTemplate;
5254
private final String testPathTemplate;
5355
private final Pattern testProjectPattern;
@@ -204,6 +206,7 @@ private String resolveGroups(String path, String tplWithGroups, String tplWithRe
204206
List<GroupRef> groupRefs = getGroupRefs(result);
205207
reverse(groupRefs);
206208

209+
StringBuilder resultBuilder = new StringBuilder(result);
207210
for (int i = 0; i < groupRefs.size(); i++)
208211
{
209212
GroupRef ref = groupRefs.get(i);
@@ -218,8 +221,9 @@ private String resolveGroups(String path, String tplWithGroups, String tplWithRe
218221
throw new DoesNotMatchConfigurationException(analizedPath);
219222
}
220223

221-
result = result.substring(0, ref.startIdx) + groupContent + result.substring(ref.endIdx);
224+
resultBuilder.replace(ref.startIdx, ref.endIdx, groupContent);
222225
}
226+
result = resultBuilder.toString();
223227
}
224228
return result;
225229
}
@@ -251,6 +255,11 @@ public SourceFolderPath getSrcPathFor(Path testPath) throws DoesNotMatchConfigur
251255

252256
private String replaceGroupsWithRefs(String template, List<GroupRef> groupRefs)
253257
{
258+
if (groupRefs.isEmpty())
259+
{
260+
return template;
261+
}
262+
254263
Map<Integer, Integer> refIndices = new HashMap<Integer, Integer>();
255264
int idx = 1;
256265
for (GroupRef ref : groupRefs)
@@ -259,12 +268,16 @@ private String replaceGroupsWithRefs(String template, List<GroupRef> groupRefs)
259268
idx++;
260269
}
261270

262-
String result = template;
263-
for (int i = 0; i < groupRefs.size(); i++)
271+
Matcher matcher = GROUP_PATTERN.matcher(template);
272+
StringBuffer sb = new StringBuffer();
273+
int i = 0;
274+
while (matcher.find() && i < groupRefs.size())
264275
{
265-
result = result.replaceFirst("\\([^\\)]+\\)", "\\\\" + refIndices.get(i));
276+
matcher.appendReplacement(sb, Matcher.quoteReplacement("\\" + refIndices.get(i)));
277+
i++;
266278
}
267-
return result;
279+
matcher.appendTail(sb);
280+
return sb.toString();
268281
}
269282

270283
private String getSrcProjectName(String tstProjectName, Path tstPath) throws DoesNotMatchConfigurationException

0 commit comments

Comments
 (0)