Skip to content

Commit

Permalink
Improve performance in CharOperation
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Stryker <stryker@redhat.com>
  • Loading branch information
Rob Stryker committed Dec 10, 2024
1 parent 8d1cb65 commit c721a10
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3750,7 +3750,7 @@ public static final char[] replace(
next : for (int i = 0; i < max;) {
int index = indexOf(toBeReplaced, array, true, i);
if (index == -1) {
i++;
i = max; // end
continue next;
}
if (occurrenceCount == starts.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,26 @@ public void test012() {
4,
true));
}

public void testReplacePerformance001() {
// This is the test that takes an excessively long time
// The improvement here is drastic, 99% reduction in time
testReplaceImpl("This is one line with no matches\n", 9, 0.01);
}

private void testReplaceImpl(String oneLine, int power, double multiplier) {
String total = oneLine;
for( int i = 0; i < power; i++ ) {
total = total + total; // Double the length
}
total = oneLine + total;

// Now replace
long start = System.currentTimeMillis();
char[] found = CharOperation.replace(total.toCharArray(), new char[]{'/'}, new char[] {'z'});
assertNotNull(found);
long end = System.currentTimeMillis();
long spent = end - start;
assertTrue(spent < 10);
}
}

0 comments on commit c721a10

Please sign in to comment.