Skip to content

Commit 173cab6

Browse files
committed
cast to boolean lazily in OrNode and AndNode alike
1 parent 02ef8d9 commit 173cab6

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/main/java/org/truffleruby/language/control/AndNode.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,19 @@ public AndNode(RubyNode left, RubyNode right) {
3434
public Object execute(VirtualFrame frame) {
3535
final Object leftValue = left.execute(frame);
3636

37-
if (leftCast == null) {
38-
CompilerDirectives.transferToInterpreterAndInvalidate();
39-
leftCast = insert(BooleanCastNodeGen.create(null));
40-
}
41-
42-
final boolean leftBoolean = leftCast.executeToBoolean(leftValue);
43-
44-
if (conditionProfile.profile(leftBoolean)) {
37+
if (conditionProfile.profile(castToBoolean(leftValue))) {
4538
return right.execute(frame);
4639
} else {
4740
return leftValue;
4841
}
4942
}
5043

44+
private boolean castToBoolean(final Object value) {
45+
if (leftCast == null) {
46+
CompilerDirectives.transferToInterpreterAndInvalidate();
47+
leftCast = insert(BooleanCastNodeGen.create(null));
48+
}
49+
return leftCast.executeToBoolean(value);
50+
}
51+
5152
}

src/main/java/org/truffleruby/language/control/OrNode.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package org.truffleruby.language.control;
1111

12+
import com.oracle.truffle.api.CompilerDirectives;
1213
import com.oracle.truffle.api.frame.VirtualFrame;
1314
import com.oracle.truffle.api.profiles.ConditionProfile;
1415
import org.truffleruby.core.cast.BooleanCastNode;
@@ -27,19 +28,25 @@ public class OrNode extends RubyNode {
2728
public OrNode(RubyNode left, RubyNode right) {
2829
this.left = left;
2930
this.right = right;
30-
leftCast = BooleanCastNodeGen.create(null);
3131
}
3232

3333
@Override
3434
public Object execute(VirtualFrame frame) {
3535
final Object leftValue = left.execute(frame);
36-
final boolean leftBoolean = leftCast.executeToBoolean(leftValue);
3736

38-
if (conditionProfile.profile(leftBoolean)) {
37+
if (conditionProfile.profile(castToBoolean(leftValue))) {
3938
return leftValue;
4039
} else {
4140
return right.execute(frame);
4241
}
4342
}
4443

44+
private boolean castToBoolean(final Object value) {
45+
if (leftCast == null) {
46+
CompilerDirectives.transferToInterpreterAndInvalidate();
47+
leftCast = insert(BooleanCastNodeGen.create(null));
48+
}
49+
return leftCast.executeToBoolean(value);
50+
}
51+
4552
}

0 commit comments

Comments
 (0)