Skip to content

Commit 2b0b3d2

Browse files
author
Rob Stryker
committed
Improve performance in CharOperation
Signed-off-by: Rob Stryker <stryker@redhat.com>
1 parent c002247 commit 2b0b3d2

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/core/compiler/CharOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3750,7 +3750,7 @@ public static final char[] replace(
37503750
next : for (int i = 0; i < max;) {
37513751
int index = indexOf(toBeReplaced, array, true, i);
37523752
if (index == -1) {
3753-
i++;
3753+
i = max; // end
37543754
continue next;
37553755
}
37563756
if (occurrenceCount == starts.length) {

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CharOperationTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,26 @@ public void test012() {
170170
4,
171171
true));
172172
}
173+
174+
public void testReplacePerformance001() {
175+
// This is the test that takes an excessively long time
176+
// The improvement here is drastic, 99% reduction in time
177+
testReplaceImpl("This is one line with no matches\n", 9, 0.01);
178+
}
179+
180+
private void testReplaceImpl(String oneLine, int power, double multiplier) {
181+
String total = oneLine;
182+
for( int i = 0; i < power; i++ ) {
183+
total = total + total; // Double the length
184+
}
185+
total = oneLine + total;
186+
187+
// Now replace
188+
long start = System.currentTimeMillis();
189+
char[] found = CharOperation.replace(total.toCharArray(), new char[]{'/'}, new char[] {'z'});
190+
assertNotNull(found);
191+
long end = System.currentTimeMillis();
192+
long spent = end - start;
193+
assertTrue(spent < 10);
194+
}
173195
}

0 commit comments

Comments
 (0)