Skip to content

Commit

Permalink
regex: don't PE String.indexOf
Browse files Browse the repository at this point in the history
  • Loading branch information
zapster committed Jul 26, 2022
1 parent 0dfce71 commit 155dd61
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import com.oracle.truffle.api.ArrayUtils;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.Specialization;
Expand Down Expand Up @@ -73,10 +74,15 @@ public int doBytesMask(byte[] input, int fromIndex, int maxIndex, byte[] match,

@Specialization(guards = "mask == null")
public int doString(String input, int fromIndex, int maxIndex, String match, @SuppressWarnings("unused") Object mask, @SuppressWarnings("unused") Encoding encoding) {
int result = input.indexOf(match, fromIndex);
int result = stringIndexOf(input, fromIndex, match);
return result >= maxIndex ? -1 : result;
}

@TruffleBoundary
private static int stringIndexOf(String input, int fromIndex, String match) {
return input.indexOf(match, fromIndex);
}

@Specialization(guards = "mask != null")
public int doStringMask(String input, int fromIndex, int maxIndex, String match, String mask, @SuppressWarnings("unused") Encoding encoding) {
return ArrayUtils.indexOfWithOrMask(input, fromIndex, maxIndex - fromIndex, match, mask);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.oracle.truffle.api.interop.UnknownIdentifierException;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import com.oracle.truffle.api.nodes.ExplodeLoop;
import com.oracle.truffle.regex.AbstractConstantKeysObject;
import com.oracle.truffle.regex.util.TruffleReadOnlyKeysArray;

Expand All @@ -71,7 +72,7 @@ private enum Mode {
public static final Mode[] VALUES = Mode.values();

public static Mode fromFlagChar(int ch) {
return VALUES[TYPE_FLAGS.indexOf(ch)];
return VALUES[indexOfUnrolled(TYPE_FLAGS, ch)];
}
}

Expand All @@ -98,7 +99,7 @@ private RubyFlags(int value, Mode mode) {
}

private static int maskForFlag(int flagChar) {
return 1 << BIT_FLAGS.indexOf(flagChar);
return 1 << indexOfUnrolled(BIT_FLAGS, flagChar);
}

public boolean hasFlag(int flagChar) {
Expand Down Expand Up @@ -151,15 +152,26 @@ public RubyFlags delFlag(int flagChar) {
}

public static boolean isValidFlagChar(int candidateChar) {
return FLAGS.indexOf(candidateChar) >= 0;
return indexOfUnrolled(FLAGS, candidateChar) >= 0;
}

public static boolean isBitFlag(int candidateChar) {
return BIT_FLAGS.indexOf(candidateChar) >= 0;
return indexOfUnrolled(BIT_FLAGS, candidateChar) >= 0;
}

public static boolean isTypeFlag(int candidateChar) {
return TYPE_FLAGS.indexOf(candidateChar) >= 0;
return indexOfUnrolled(TYPE_FLAGS, candidateChar) >= 0;
}

@ExplodeLoop
private static int indexOfUnrolled(String flags, int candidateChar) {
for (int i = 0; i < flags.length(); i++) {
int c = flags.codePointAt(i);
if (candidateChar == c) {
return i;
}
}
return -1;
}

@Override
Expand Down

0 comments on commit 155dd61

Please sign in to comment.