Skip to content

Commit c67bf5f

Browse files
committed
Refactor RescueNode so BooleanCastNode can be inlined
1 parent 1b83c7c commit c67bf5f

File tree

5 files changed

+16
-17
lines changed

5 files changed

+16
-17
lines changed

src/main/java/org/truffleruby/language/exceptions/RescueClassesNode.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.truffleruby.RubyContext;
1313
import org.truffleruby.RubyLanguage;
14+
import org.truffleruby.core.cast.BooleanCastNode;
1415
import org.truffleruby.language.RubyNode;
1516

1617
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -27,10 +28,10 @@ public RescueClassesNode(RubyNode[] handlingClassNodes, RubyNode rescueBody) {
2728

2829
@ExplodeLoop
2930
@Override
30-
public boolean canHandle(VirtualFrame frame, Object exceptionObject) {
31+
public boolean canHandle(VirtualFrame frame, Object exceptionObject, BooleanCastNode booleanCastNode) {
3132
for (RubyNode handlingClassNode : handlingClassNodes) {
3233
final Object handlingClass = handlingClassNode.execute(frame);
33-
if (matches(exceptionObject, handlingClass)) {
34+
if (matches(exceptionObject, handlingClass, booleanCastNode)) {
3435
return true;
3536
}
3637
}

src/main/java/org/truffleruby/language/exceptions/RescueNode.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.truffleruby.RubyContext;
1414
import org.truffleruby.RubyLanguage;
1515
import org.truffleruby.core.cast.BooleanCastNode;
16-
import org.truffleruby.core.cast.BooleanCastNodeGen;
1716
import org.truffleruby.core.module.RubyModule;
1817
import org.truffleruby.language.RubyContextSourceNode;
1918
import org.truffleruby.language.RubyNode;
@@ -32,7 +31,6 @@ public abstract class RescueNode extends RubyContextSourceNode {
3231
@Child private RubyNode rescueBody;
3332

3433
@Child private DispatchNode callTripleEqualsNode;
35-
@Child private BooleanCastNode booleanCastNode;
3634
@Child private InteropLibrary interopLibrary;
3735

3836
private final BranchProfile errorProfile = BranchProfile.create();
@@ -46,14 +44,14 @@ protected RescueNode() {
4644
this.rescueBody = null;
4745
}
4846

49-
public abstract boolean canHandle(VirtualFrame frame, Object exceptionObject);
47+
public abstract boolean canHandle(VirtualFrame frame, Object exceptionObject, BooleanCastNode booleanCastNode);
5048

5149
@Override
5250
public Object execute(VirtualFrame frame) {
5351
return rescueBody.execute(frame);
5452
}
5553

56-
protected boolean matches(Object exceptionObject, Object handlingClass) {
54+
protected boolean matches(Object exceptionObject, Object handlingClass, BooleanCastNode booleanCastNode) {
5755
if (!(handlingClass instanceof RubyModule)) {
5856
if (interopLibrary == null) {
5957
CompilerDirectives.transferToInterpreterAndInvalidate();
@@ -70,10 +68,6 @@ protected boolean matches(Object exceptionObject, Object handlingClass) {
7068
CompilerDirectives.transferToInterpreterAndInvalidate();
7169
callTripleEqualsNode = insert(DispatchNode.create());
7270
}
73-
if (booleanCastNode == null) {
74-
CompilerDirectives.transferToInterpreterAndInvalidate();
75-
booleanCastNode = insert(BooleanCastNodeGen.create(null));
76-
}
7771

7872
final Object matches = callTripleEqualsNode.call(handlingClass, "===", exceptionObject);
7973
return booleanCastNode.execute(matches);

src/main/java/org/truffleruby/language/exceptions/RescueSplatNode.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.truffleruby.RubyLanguage;
1616
import org.truffleruby.core.array.RubyArray;
1717
import org.truffleruby.core.array.library.ArrayStoreLibrary;
18+
import org.truffleruby.core.cast.BooleanCastNode;
1819
import org.truffleruby.core.cast.SplatCastNode;
1920
import org.truffleruby.core.cast.SplatCastNodeGen;
2021
import org.truffleruby.language.RubyNode;
@@ -39,13 +40,13 @@ public RescueSplatNode(RubyLanguage language, RubyNode handlingClassesArray, Rub
3940
}
4041

4142
@Override
42-
public boolean canHandle(VirtualFrame frame, Object exceptionObject) {
43+
public boolean canHandle(VirtualFrame frame, Object exceptionObject, BooleanCastNode booleanCastNode) {
4344
final RubyArray handlingClasses = (RubyArray) splatCastNode.execute(frame);
4445

4546
int i = 0;
4647
try {
4748
for (; loopProfile.inject(i < handlingClasses.size); ++i) {
48-
if (matches(exceptionObject, stores.read(handlingClasses.getStore(), i))) {
49+
if (matches(exceptionObject, stores.read(handlingClasses.getStore(), i), booleanCastNode)) {
4950
return true;
5051
}
5152
TruffleSafepoint.poll(this);

src/main/java/org/truffleruby/language/exceptions/RescueStandardErrorNode.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.truffleruby.RubyContext;
1313
import org.truffleruby.RubyLanguage;
14+
import org.truffleruby.core.cast.BooleanCastNode;
1415
import org.truffleruby.language.RubyNode;
1516

1617
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -22,8 +23,8 @@ public RescueStandardErrorNode(RubyNode rescueBody) {
2223
}
2324

2425
@Override
25-
public boolean canHandle(VirtualFrame frame, Object exceptionObject) {
26-
return matches(exceptionObject, coreLibrary().standardErrorClass);
26+
public boolean canHandle(VirtualFrame frame, Object exceptionObject, BooleanCastNode booleanCastNode) {
27+
return matches(exceptionObject, coreLibrary().standardErrorClass, booleanCastNode);
2728
}
2829

2930
@Override

src/main/java/org/truffleruby/language/exceptions/TryNode.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.oracle.truffle.api.TruffleSafepoint;
1818
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
1919
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
20+
import org.truffleruby.core.cast.BooleanCastNode;
2021
import org.truffleruby.core.exception.ExceptionOperations;
2122
import org.truffleruby.language.RubyContextSourceNode;
2223
import org.truffleruby.language.RubyNode;
@@ -58,6 +59,7 @@ protected Object doTry(VirtualFrame frame,
5859
@Cached InlinedBranchProfile killExceptionProfile,
5960
@Cached InlinedBranchProfile guestExceptionProfile,
6061
@Cached InlinedBranchProfile retryProfile,
62+
@Cached BooleanCastNode booleanCastNode,
6163
@Cached InlinedConditionProfile raiseExceptionProfile) {
6264
while (true) {
6365
Object result;
@@ -71,7 +73,7 @@ protected Object doTry(VirtualFrame frame,
7173
} catch (AbstractTruffleException exception) {
7274
guestExceptionProfile.enter(this);
7375
try {
74-
return handleException(frame, exception, raiseExceptionProfile);
76+
return handleException(frame, exception, raiseExceptionProfile, booleanCastNode);
7577
} catch (RetryException e) {
7678
retryProfile.enter(this);
7779
TruffleSafepoint.poll(this);
@@ -95,11 +97,11 @@ protected Object doTry(VirtualFrame frame,
9597

9698
@ExplodeLoop(kind = LoopExplosionKind.FULL_UNROLL_UNTIL_RETURN)
9799
private Object handleException(VirtualFrame frame, AbstractTruffleException exception,
98-
InlinedConditionProfile raiseExceptionProfile) {
100+
InlinedConditionProfile raiseExceptionProfile, BooleanCastNode booleanCastNode) {
99101
final Object exceptionObject = ExceptionOperations.getExceptionObject(this, exception, raiseExceptionProfile);
100102

101103
for (RescueNode rescue : rescueParts) {
102-
if (rescue.canHandle(frame, exceptionObject)) {
104+
if (rescue.canHandle(frame, exceptionObject, booleanCastNode)) {
103105
if (getContext().getOptions().BACKTRACE_ON_RESCUE) {
104106
printBacktraceOnRescue(rescue, exception);
105107
}

0 commit comments

Comments
 (0)