Skip to content

Commit

Permalink
TRegex: fix performance of readMember in RegexResult
Browse files Browse the repository at this point in the history
  • Loading branch information
djoooooe committed Jan 8, 2021
1 parent cbffbd2 commit ed7b5a1
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 14 deletions.
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.profiles.ValueProfile;
import com.oracle.truffle.regex.util.TruffleReadOnlyKeysArray;

@ExportLibrary(InteropLibrary.class)
Expand Down Expand Up @@ -96,19 +97,22 @@ public abstract static class ReadMember {

@Specialization(guards = "symbol == cachedSymbol", limit = "8")
public static Object readIdentity(AbstractConstantKeysObject receiver, @SuppressWarnings("unused") String symbol,
@Cached("symbol") String cachedSymbol) throws UnknownIdentifierException {
return read(receiver, cachedSymbol);
@Cached("symbol") String cachedSymbol,
@Cached("createClassProfile()") ValueProfile classProfile) throws UnknownIdentifierException {
return read(receiver, cachedSymbol, classProfile);
}

@Specialization(guards = "symbol.equals(cachedSymbol)", limit = "8", replaces = "readIdentity")
public static Object readEquals(AbstractConstantKeysObject receiver, @SuppressWarnings("unused") String symbol,
@Cached("symbol") String cachedSymbol) throws UnknownIdentifierException {
return read(receiver, cachedSymbol);
@Cached("symbol") String cachedSymbol,
@Cached("createClassProfile()") ValueProfile classProfile) throws UnknownIdentifierException {
return read(receiver, cachedSymbol, classProfile);
}

@Specialization(replaces = "readEquals")
public static Object read(AbstractConstantKeysObject receiver, String symbol) throws UnknownIdentifierException {
return receiver.readMemberImpl(symbol);
public static Object read(AbstractConstantKeysObject receiver, String symbol,
@Cached("createClassProfile()") ValueProfile classProfile) throws UnknownIdentifierException {
return classProfile.profile(receiver).readMemberImpl(symbol);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,20 +248,21 @@ Object invokeMember(String member, Object[] args,
@GenerateUncached
abstract static class InvokeCacheNode extends Node {

abstract Object execute(String symbol, CallTarget receiver, Object input, int fromIndex) throws UnsupportedMessageException, ArityException, UnsupportedTypeException, UnknownIdentifierException;
abstract Object execute(String symbol, CallTarget receiver, Object input, int fromIndex)
throws UnsupportedMessageException, ArityException, UnsupportedTypeException, UnknownIdentifierException;

@SuppressWarnings("unused")
@Specialization(guards = {"symbol == cachedSymbol", "cachedSymbol.equals(PROP_EXEC)"}, limit = N_METHODS)
Object getStartIdentity(String symbol, CallTarget receiver, Object input, int fromIndex,
Object execIdentity(String symbol, CallTarget receiver, Object input, int fromIndex,
@Cached("symbol") String cachedSymbol,
@Cached ExpectStringOrTruffleObjectNode expectStringOrTruffleObjectNode,
@Cached ExecCompiledRegexNode execNode) throws UnsupportedMessageException, ArityException, UnsupportedTypeException {
return execNode.execute(receiver, expectStringOrTruffleObjectNode.execute(input), fromIndex);
}

@SuppressWarnings("unused")
@Specialization(guards = {"symbol.equals(cachedSymbol)", "cachedSymbol.equals(PROP_EXEC)"}, limit = N_METHODS, replaces = "getStartIdentity")
Object getStartEquals(String symbol, CallTarget receiver, Object input, int fromIndex,
@Specialization(guards = {"symbol.equals(cachedSymbol)", "cachedSymbol.equals(PROP_EXEC)"}, limit = N_METHODS, replaces = "execIdentity")
Object execEquals(String symbol, CallTarget receiver, Object input, int fromIndex,
@Cached("symbol") String cachedSymbol,
@Cached ExpectStringOrTruffleObjectNode expectStringOrTruffleObjectNode,
@Cached ExecCompiledRegexNode execNode) throws UnsupportedMessageException, ArityException, UnsupportedTypeException {
Expand All @@ -271,7 +272,7 @@ Object getStartEquals(String symbol, CallTarget receiver, Object input, int from
// EXPERIMENTAL
@SuppressWarnings("unused")
@Specialization(guards = {"symbol == cachedSymbol", "cachedSymbol.equals(PROP_EXEC_BYTES)"}, limit = N_METHODS)
Object getEndIdentity(String symbol, CallTarget receiver, Object input, int fromIndex,
Object execBytesIdentity(String symbol, CallTarget receiver, Object input, int fromIndex,
@Cached("symbol") String cachedSymbol,
@Cached ExpectByteArrayHostObjectNode expectByteArrayHostObjectNode,
@Cached ExecCompiledRegexNode execNode) throws UnsupportedMessageException, ArityException, UnsupportedTypeException {
Expand All @@ -280,16 +281,16 @@ Object getEndIdentity(String symbol, CallTarget receiver, Object input, int from

// EXPERIMENTAL
@SuppressWarnings("unused")
@Specialization(guards = {"symbol.equals(cachedSymbol)", "cachedSymbol.equals(PROP_EXEC_BYTES)"}, limit = N_METHODS, replaces = "getEndIdentity")
Object getEndEquals(String symbol, CallTarget receiver, Object input, int fromIndex,
@Specialization(guards = {"symbol.equals(cachedSymbol)", "cachedSymbol.equals(PROP_EXEC_BYTES)"}, limit = N_METHODS, replaces = "execBytesIdentity")
Object execBytesEquals(String symbol, CallTarget receiver, Object input, int fromIndex,
@Cached("symbol") String cachedSymbol,
@Cached ExpectByteArrayHostObjectNode expectByteArrayHostObjectNode,
@Cached ExecCompiledRegexNode execNode) throws UnsupportedMessageException, ArityException, UnsupportedTypeException {
return execNode.execute(receiver, expectByteArrayHostObjectNode.execute(input), fromIndex);
}

@ReportPolymorphism.Megamorphic
@Specialization(replaces = {"getStartEquals", "getEndEquals"})
@Specialization(replaces = {"execEquals", "execBytesEquals"})
static Object invokeGeneric(String symbol, CallTarget receiver, Object input, int fromIndex,
@Cached ExpectStringOrTruffleObjectNode expectStringOrTruffleObjectNode,
@Cached ExpectByteArrayHostObjectNode expectByteArrayHostObjectNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
*/
package com.oracle.truffle.regex.result;

import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
Expand All @@ -51,13 +52,16 @@
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnknownIdentifierException;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.interop.UnsupportedTypeException;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.regex.AbstractConstantKeysObject;
import com.oracle.truffle.regex.AbstractRegexObject;
import com.oracle.truffle.regex.RegexObject;
import com.oracle.truffle.regex.runtime.nodes.ExpectByteArrayHostObjectNode;
import com.oracle.truffle.regex.runtime.nodes.ExpectStringOrTruffleObjectNode;
import com.oracle.truffle.regex.runtime.nodes.ToIntNode;
import com.oracle.truffle.regex.util.TruffleReadOnlyKeysArray;

Expand Down Expand Up @@ -92,6 +96,99 @@ public abstract class RegexResult extends AbstractConstantKeysObject {

public abstract int getEnd(int groupNumber);

@ExportMessage
public Object getMembers(@SuppressWarnings("unused") boolean includeInternal) {
return KEYS;
}

@ExportMessage
abstract static class ReadMember {

@SuppressWarnings("unused")
@Specialization(guards = {"symbol == cachedSymbol", "cachedSymbol.equals(PROP_IS_MATCH)"}, limit = "2")
static boolean isMatchIdentity(RegexResult receiver, String symbol,
@Cached("symbol") String cachedSymbol) {
return receiver != NoMatchResult.getInstance();
}

@SuppressWarnings("unused")
@Specialization(guards = {"symbol.equals(cachedSymbol)", "cachedSymbol.equals(PROP_IS_MATCH)"}, limit = "2", replaces = "isMatchIdentity")
static boolean isMatchEquals(RegexResult receiver, String symbol,
@Cached("symbol") String cachedSymbol) {
return receiver != NoMatchResult.getInstance();
}

@SuppressWarnings("unused")
@Specialization(guards = {"symbol == cachedSymbol", "cachedSymbol.equals(PROP_GET_START)"}, limit = "2")
static RegexResultGetStartMethod getStartIdentity(RegexResult receiver, String symbol,
@Cached("symbol") String cachedSymbol) {
return new RegexResultGetStartMethod(receiver);
}

@SuppressWarnings("unused")
@Specialization(guards = {"symbol.equals(cachedSymbol)", "cachedSymbol.equals(PROP_GET_START)"}, limit = "2", replaces = "getStartIdentity")
static RegexResultGetStartMethod getStartEquals(RegexResult receiver, String symbol,
@Cached("symbol") String cachedSymbol) {
return new RegexResultGetStartMethod(receiver);
}

@SuppressWarnings("unused")
@Specialization(guards = {"symbol == cachedSymbol", "cachedSymbol.equals(PROP_GET_END)"}, limit = "2")
static RegexResultGetEndMethod getEndIdentity(RegexResult receiver, String symbol,
@Cached("symbol") String cachedSymbol) {
return new RegexResultGetEndMethod(receiver);
}

@SuppressWarnings("unused")
@Specialization(guards = {"symbol.equals(cachedSymbol)", "cachedSymbol.equals(PROP_GET_END)"}, limit = "2", replaces = "getEndIdentity")
static RegexResultGetEndMethod getEndEquals(RegexResult receiver, String symbol,
@Cached("symbol") String cachedSymbol) {
return new RegexResultGetEndMethod(receiver);
}

@ReportPolymorphism.Megamorphic
@Specialization(replaces = {"isMatchEquals", "getStartEquals", "getEndEquals"})
static Object readGeneric(RegexResult receiver, String symbol) throws UnknownIdentifierException {
switch (symbol) {
case PROP_IS_MATCH:
return receiver != NoMatchResult.getInstance();
case PROP_GET_START:
return new RegexResultGetStartMethod(receiver);
case PROP_GET_END:
return new RegexResultGetEndMethod(receiver);
default:
CompilerDirectives.transferToInterpreterAndInvalidate();
throw UnknownIdentifierException.create(symbol);
}
}
}

@ExportMessage
abstract static class IsMemberReadable {

@SuppressWarnings("unused")
@Specialization(guards = {"symbol == cachedSymbol", "result"}, limit = "3")
static boolean cacheIdentity(RegexResult receiver, String symbol,
@Cached("symbol") String cachedSymbol,
@Cached("isReadable(receiver, cachedSymbol)") boolean result) {
return result;
}

@SuppressWarnings("unused")
@Specialization(guards = {"symbol.equals(cachedSymbol)", "result"}, limit = "3", replaces = "cacheIdentity")
static boolean cacheEquals(RegexResult receiver, String symbol,
@Cached("symbol") String cachedSymbol,
@Cached("isReadable(receiver, cachedSymbol)") boolean result) {
return result;
}

@SuppressWarnings("unused")
@Specialization(replaces = "cacheEquals")
static boolean isReadable(RegexResult receiver, String symbol) {
return KEYS.contains(symbol);
}
}

@Override
public TruffleReadOnlyKeysArray getKeys() {
return KEYS;
Expand Down

0 comments on commit ed7b5a1

Please sign in to comment.