Skip to content

Commit ecbd7c5

Browse files
committed
Refactor loop in SpecialVariablesSendingNode
1 parent 6ea0387 commit ecbd7c5

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

src/main/java/org/truffleruby/language/SpecialVariablesSendingNode.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import org.truffleruby.core.kernel.TruffleKernelNodes.GetSpecialVariableStorage;
1717
import org.truffleruby.language.arguments.ReadCallerVariablesNode;
1818

19+
import org.truffleruby.language.locals.FindDeclarationVariableNodes;
1920
import org.truffleruby.language.threadlocal.SpecialVariableStorage;
20-
import org.truffleruby.parser.ParentFrameDescriptor;
2121

2222
/** Some Ruby methods need access to the caller special variables: see usages of {@link ReadCallerVariablesNode}. This
2323
* is used for methods which need to access the last regexp MatchData or the last IO line.
@@ -42,23 +42,16 @@ public abstract class SpecialVariablesSendingNode extends RubyBaseNode {
4242
@NeverDefault
4343
protected Assumption getSpecialVariableAssumption(Frame frame) {
4444
if (frame == null) {
45-
return getValidAssumption();
45+
return Assumption.ALWAYS_VALID;
4646
}
47-
var currentFrameDescriptor = frame.getFrameDescriptor();
4847

49-
while (true) {
50-
if (currentFrameDescriptor.getInfo() == null) {
51-
return getValidAssumption();
52-
}
53-
if (currentFrameDescriptor.getInfo() instanceof ParentFrameDescriptor nextDescriptor) {
54-
currentFrameDescriptor = nextDescriptor.getDescriptor();
55-
} else {
56-
return (Assumption) currentFrameDescriptor.getInfo();
57-
}
48+
var outerFrameDescriptor = FindDeclarationVariableNodes.getOuterFrameDescriptor(frame.getFrameDescriptor());
49+
50+
if (SpecialVariableStorage.hasSpecialVariableAssumption(outerFrameDescriptor)) {
51+
return SpecialVariableStorage.getAssumption(outerFrameDescriptor);
52+
} else {
53+
return Assumption.ALWAYS_VALID;
5854
}
5955
}
6056

61-
protected Assumption getValidAssumption() {
62-
return Assumption.ALWAYS_VALID;
63-
}
6457
}

src/main/java/org/truffleruby/language/dispatch/DispatchNode.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.oracle.truffle.api.RootCallTarget;
1515
import com.oracle.truffle.api.dsl.Cached;
1616
import com.oracle.truffle.api.dsl.GenerateUncached;
17+
import com.oracle.truffle.api.dsl.ImportStatic;
1718
import com.oracle.truffle.api.dsl.NeverDefault;
1819
import com.oracle.truffle.api.dsl.Specialization;
1920
import com.oracle.truffle.api.frame.Frame;
@@ -37,6 +38,7 @@
3738

3839
import static org.truffleruby.language.dispatch.DispatchConfiguration.PRIVATE;
3940

41+
@ImportStatic(Assumption.class)
4042
@GenerateUncached
4143
public abstract class DispatchNode extends SpecialVariablesSendingNode {
4244

@@ -280,7 +282,7 @@ protected Object dispatch(
280282
DispatchConfiguration config,
281283
LiteralCallNode literalCallNode,
282284
@Cached(value = "getSpecialVariableAssumption(frame)",
283-
uncached = "getValidAssumption()") Assumption specialVariableAssumption,
285+
uncached = "ALWAYS_VALID") Assumption specialVariableAssumption,
284286
@Cached MetaClassNode metaclassNode,
285287
@Cached LookupMethodNode methodLookup,
286288
@Cached InlinedConditionProfile methodMissing,

src/main/java/org/truffleruby/language/locals/FindDeclarationVariableNodes.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.oracle.truffle.api.dsl.ReportPolymorphism;
2626
import com.oracle.truffle.api.dsl.Specialization;
2727
import com.oracle.truffle.api.frame.FrameDescriptor;
28+
import org.truffleruby.parser.ParentFrameDescriptor;
2829

2930
public abstract class FindDeclarationVariableNodes {
3031
public static final class FrameSlotAndDepth {
@@ -49,6 +50,13 @@ public static MaterializedFrame getOuterDeclarationFrame(MaterializedFrame topFr
4950
return frame;
5051
}
5152

53+
public static FrameDescriptor getOuterFrameDescriptor(FrameDescriptor descriptor) {
54+
while (descriptor.getInfo() instanceof ParentFrameDescriptor nextDescriptor) {
55+
descriptor = nextDescriptor.getDescriptor();
56+
}
57+
return descriptor;
58+
}
59+
5260
private static int findSlot(FrameDescriptor descriptor, String name) {
5361
assert descriptor.getNumberOfAuxiliarySlots() == 0;
5462
int slots = descriptor.getNumberOfSlots();

src/main/java/org/truffleruby/language/threadlocal/SpecialVariableStorage.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public static Assumption getAssumption(FrameDescriptor descriptor) {
5151
return (Assumption) descriptor.getInfo();
5252
}
5353

54+
public static boolean hasSpecialVariableAssumption(FrameDescriptor descriptor) {
55+
var info = descriptor.getInfo();
56+
return info instanceof Assumption assumption && assumption.getName() == ASSUMPTION_NAME;
57+
}
58+
5459
public static boolean hasSpecialVariableStorageSlot(Frame frame) {
5560
assert RubyArguments.getDeclarationFrame(frame) == null;
5661
return hasSpecialVariableStorageSlot(frame.getFrameDescriptor());

0 commit comments

Comments
 (0)