Skip to content

Commit 30956fb

Browse files
author
Nicolas Laurent
committed
refactor & clarify FrameSendingNode
reb framesending
1 parent d82e230 commit 30956fb

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

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

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
package org.truffleruby.language;
1111

1212
import com.oracle.truffle.api.CompilerDirectives;
13+
import com.oracle.truffle.api.frame.Frame;
1314
import com.oracle.truffle.api.nodes.InvalidAssumptionException;
15+
import org.truffleruby.core.binding.RubyBinding;
1416
import org.truffleruby.language.arguments.ReadCallerFrameNode;
1517

1618
import com.oracle.truffle.api.Assumption;
@@ -19,20 +21,51 @@
1921
import com.oracle.truffle.api.frame.VirtualFrame;
2022
import com.oracle.truffle.api.nodes.Node;
2123
import com.oracle.truffle.api.utilities.AlwaysValidAssumption;
24+
import org.truffleruby.language.methods.DeclarationContext;
2225

26+
/** Some Ruby methods need access to the caller frame (the frame active when the method call was made): see usages of
27+
* {@link ReadCallerFrameNode}. This is notably used to get hold of instances of {@link DeclarationContext} and
28+
* {@link RubyBinding}.
29+
*
30+
* <p>
31+
* This means that when making a method call, we might need to pass down its {@link Frame} active when the method call
32+
* was made.
33+
*
34+
* <p>
35+
* When retrieving the frame in a method called through the Ruby {@code #send} method, we must not retrieve the frame of
36+
* the actual call (made by {@code #send}) but the frame of the {@code #send} call itself.
37+
*
38+
* <p>
39+
* Materializing a frame is expensive, and the point of this parent node is to only materialize the frame when we know
40+
* for sure it has been requested by the callee. It is also possible to walk the stack to retrieve the frame to
41+
* materialize - but this is even slower and causes a deoptimization in the callee.
42+
*
43+
* <p>
44+
* This class works in tandem with {@link ReadCallerFrameNode} for this purpose. At first, we don't send down the frame.
45+
* If the callee needs it, it will de-optimize and walk the stack to retrieve it (slow). It will also call
46+
* {@link #startSendingOwnFrame()}}, so that the next time the method is called, the frame will be passed down and the
47+
* method does not need further de-optimizations. (Note in the case of {@code #send} calls, we need to recursively call
48+
* {@link ReadCallerFrameNode} to get the parent frame!)
49+
*
50+
* <p>
51+
* This class is the sole consumer of {@link RubyRootNode#getNeedsCallerAssumption()}, which is used to optimize
52+
* {@link #getFrameIfRequired(VirtualFrame)} (called by subclasses in order to pass down the frame or not). Starting to
53+
* send the frame invalidates the assumption. In other words, the assumption guards the fact that {@link #sendsFrame} is
54+
* a compilation constant, and is invalidated whenever it needs to change. */
2355
public abstract class FrameSendingNode extends RubyContextNode {
2456

25-
protected enum SendsFrame {
26-
NO_FRAME,
27-
MY_FRAME,
28-
CALLER_FRAME;
57+
private enum SendsFrame {
58+
NO_FRAME, // callees don't need to read the frame
59+
MY_FRAME, // for most calls
60+
CALLER_FRAME; // for `send` calls
2961
}
3062

3163
@CompilationFinal protected SendsFrame sendsFrame = SendsFrame.NO_FRAME;
3264
@CompilationFinal protected Assumption needsCallerAssumption;
3365

3466
@Child protected ReadCallerFrameNode readCaller;
3567

68+
/** Whether we are sending down the frame (because the called method reads it). */
3669
protected boolean sendingFrames() {
3770
return sendsFrame != SendsFrame.NO_FRAME;
3871
}
@@ -52,7 +85,7 @@ private synchronized void startSendingFrame(SendsFrame frameToSend) {
5285
}
5386

5487
// We'd only get AlwaysValidAssumption if the root node isn't Ruby (in which case this shouldn't be called),
55-
// or when we already know to send the frame (in which case we'd have exited above.
88+
// or when we already know to send the frame (in which case we'd have exited above).
5689
assert needsCallerAssumption != AlwaysValidAssumption.INSTANCE;
5790

5891
this.sendsFrame = frameToSend;
@@ -67,7 +100,7 @@ private synchronized void startSendingFrame(SendsFrame frameToSend) {
67100
}
68101
}
69102

70-
protected synchronized void resetNeedsCallerAssumption() {
103+
private synchronized void resetNeedsCallerAssumption() {
71104
Node root = getRootNode();
72105
if (root instanceof RubyRootNode && !sendingFrames()) {
73106
needsCallerAssumption = ((RubyRootNode) root).getNeedsCallerAssumption();
@@ -76,9 +109,8 @@ protected synchronized void resetNeedsCallerAssumption() {
76109
}
77110
}
78111

79-
public MaterializedFrame getFrameIfRequiredNew(VirtualFrame frame) {
80-
// TODO(norswap, 07 Aug 2020): worth moving up to the dispatch node & profiling?
81-
if (frame == null) {
112+
public MaterializedFrame getFrameIfRequired(VirtualFrame frame) {
113+
if (frame == null) { // the frame should be proved null or non-null at PE time
82114
return null;
83115
}
84116

@@ -102,15 +134,4 @@ public MaterializedFrame getFrameIfRequiredNew(VirtualFrame frame) {
102134
return null;
103135
}
104136
}
105-
106-
public MaterializedFrame getFrameIfRequired(VirtualFrame frame) {
107-
switch (sendsFrame) {
108-
case MY_FRAME:
109-
return frame.materialize();
110-
case CALLER_FRAME:
111-
return readCaller.execute(frame);
112-
default:
113-
return null;
114-
}
115-
}
116137
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public Object execute(VirtualFrame frame, Object receiver, Object methodName, Ru
169169
return method.isImplemented();
170170
}
171171

172-
final MaterializedFrame callerFrame = getFrameIfRequiredNew(frame);
172+
final MaterializedFrame callerFrame = getFrameIfRequired(frame);
173173
final Object[] frameArguments = RubyArguments.pack(null, callerFrame, method, null, receiver, block, arguments);
174174

175175
return callNode.execute(method, frameArguments);

0 commit comments

Comments
 (0)