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>

Quality gate cleanup

Signed-off-by: Rob Stryker <stryker@redhat.com>
  • Loading branch information
Rob Stryker committed Dec 4, 2024
1 parent c002247 commit fb37692
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3734,59 +3734,15 @@ public static final void replace(char[] array, char[] toBeReplaced, char replace
* @throws NullPointerException if the given array is null
*/
public static final char[] replace(
char[] array,
char[] toBeReplaced,
char[] replacementChars) {

int max = array.length;
int replacedLength = toBeReplaced.length;
int replacementLength = replacementChars.length;

int[] starts = new int[5];
int occurrenceCount = 0;

if (!equals(toBeReplaced, replacementChars)) {

next : for (int i = 0; i < max;) {
int index = indexOf(toBeReplaced, array, true, i);
if (index == -1) {
i++;
continue next;
}
if (occurrenceCount == starts.length) {
System.arraycopy(
starts,
0,
starts = new int[occurrenceCount * 2],
0,
occurrenceCount);
}
starts[occurrenceCount++] = index;
i = index + replacedLength;
char[] array,
char[] toBeReplaced,
char[] replacementChars) {
String s1 = new String(array);
String myRet = s1.replace(new String(toBeReplaced), new String(replacementChars));
if( myRet.equals(s1)) {
return array;
}
}
if (occurrenceCount == 0)
return array;
char[] result =
new char[max
+ occurrenceCount * (replacementLength - replacedLength)];
int inStart = 0, outStart = 0;
for (int i = 0; i < occurrenceCount; i++) {
int offset = starts[i] - inStart;
System.arraycopy(array, inStart, result, outStart, offset);
inStart += offset;
outStart += offset;
System.arraycopy(
replacementChars,
0,
result,
outStart,
replacementLength);
inStart += replacedLength;
outStart += replacementLength;
}
System.arraycopy(array, inStart, result, outStart, max - inStart);
return result;
return myRet.toCharArray();
}

/**
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 fb37692

Please sign in to comment.