Skip to content

Commit efac4e8

Browse files
committed
Refactor FiberTransferNode so it can be DSL inlined
1 parent 2075fb1 commit efac4e8

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

src/main/java/org/truffleruby/core/fiber/FiberNodes.java

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@
2020
import org.truffleruby.builtins.PrimitiveArrayArgumentsNode;
2121
import org.truffleruby.core.array.RubyArray;
2222
import org.truffleruby.core.cast.SingleValueCastNode;
23-
import org.truffleruby.core.cast.SingleValueCastNodeGen;
2423
import org.truffleruby.core.encoding.Encodings;
2524
import org.truffleruby.core.exception.RubyException;
26-
import org.truffleruby.core.fiber.FiberNodesFactory.FiberTransferNodeFactory;
2725
import org.truffleruby.core.fiber.RubyFiber.FiberStatus;
2826
import org.truffleruby.core.klass.RubyClass;
2927
import org.truffleruby.core.proc.RubyProc;
3028
import org.truffleruby.core.string.RubyString;
3129
import org.truffleruby.core.thread.RubyThread;
3230
import org.truffleruby.language.Nil;
3331
import org.truffleruby.annotations.Visibility;
32+
import org.truffleruby.language.RubyBaseNode;
3433
import org.truffleruby.language.arguments.ArgumentsDescriptor;
3534
import org.truffleruby.language.arguments.EmptyArgumentsDescriptor;
3635
import org.truffleruby.language.arguments.RubyArguments;
@@ -45,21 +44,22 @@
4544
@CoreModule(value = "Fiber", isClass = true)
4645
public abstract class FiberNodes {
4746

48-
public abstract static class FiberTransferNode extends CoreMethodArrayArgumentsNode {
47+
public abstract static class FiberTransferNodeAST extends CoreMethodArrayArgumentsNode {
4948

50-
public static FiberTransferNode create() {
51-
return FiberTransferNodeFactory.create(null);
49+
@Specialization
50+
protected Object transfer(
51+
RubyFiber currentFiber,
52+
RubyFiber toFiber,
53+
FiberOperation operation,
54+
ArgumentsDescriptor descriptor,
55+
Object[] args,
56+
@Cached FiberTransferNode fiberTransferNode) {
57+
return fiberTransferNode.execute(currentFiber, toFiber, operation, descriptor, args);
5258
}
5359

54-
@Child private SingleValueCastNode singleValueCastNode;
60+
}
5561

56-
public Object singleValue(Object[] args) {
57-
if (singleValueCastNode == null) {
58-
CompilerDirectives.transferToInterpreterAndInvalidate();
59-
singleValueCastNode = insert(SingleValueCastNodeGen.create());
60-
}
61-
return singleValueCastNode.executeSingleValue(args);
62-
}
62+
public abstract static class FiberTransferNode extends RubyBaseNode {
6363

6464
public abstract Object execute(RubyFiber currentFiber, RubyFiber toFiber, FiberOperation operation,
6565
ArgumentsDescriptor descriptor, Object[] args);
@@ -71,6 +71,7 @@ protected Object transfer(
7171
FiberOperation operation,
7272
ArgumentsDescriptor descriptor,
7373
Object[] args,
74+
@Cached SingleValueCastNode singleValueCastNode,
7475
@Cached InlinedBranchProfile errorProfile) {
7576

7677
if (toFiber.isTerminated()) {
@@ -88,7 +89,7 @@ protected Object transfer(
8889
var descriptorAndArgs = getContext().fiberManager.transferControlTo(currentFiber, toFiber, operation,
8990
descriptor, args, this);
9091
// Ignore the descriptor like CRuby here, see https://bugs.ruby-lang.org/issues/18621
91-
return singleValue(descriptorAndArgs.args);
92+
return singleValueCastNode.executeSingleValue(descriptorAndArgs.args);
9293
}
9394

9495
}
@@ -143,10 +144,10 @@ protected Object noBlock(RubyFiber fiber, boolean blocking, Nil block) {
143144
@CoreMethod(names = "transfer", rest = true)
144145
public abstract static class TransferNode extends CoreMethodArrayArgumentsNode {
145146

146-
@Child private FiberTransferNode fiberTransferNode = FiberTransferNodeFactory.create(null);
147-
148147
@Specialization
149148
protected Object transfer(VirtualFrame frame, RubyFiber toFiber, Object[] rawArgs,
149+
@Cached FiberTransferNode fiberTransferNode,
150+
@Cached SingleValueCastNode singleValueCastNode,
150151
@Cached InlinedConditionProfile sameFiberProfile,
151152
@Cached InlinedBranchProfile errorProfile) {
152153

@@ -166,7 +167,7 @@ protected Object transfer(VirtualFrame frame, RubyFiber toFiber, Object[] rawArg
166167

167168
if (sameFiberProfile.profile(this, currentFiber == toFiber)) {
168169
// A Fiber can transfer to itself
169-
return fiberTransferNode.singleValue(rawArgs);
170+
return singleValueCastNode.executeSingleValue(rawArgs);
170171
}
171172

172173
return fiberTransferNode.execute(currentFiber, toFiber, FiberOperation.TRANSFER,
@@ -185,11 +186,10 @@ public static FiberResumeNode create() {
185186
public abstract Object executeResume(FiberOperation operation, RubyFiber fiber, ArgumentsDescriptor descriptor,
186187
Object[] args);
187188

188-
@Child private FiberTransferNode fiberTransferNode = FiberTransferNodeFactory.create(null);
189-
190189
@Specialization
191190
protected Object resume(
192191
FiberOperation operation, RubyFiber toFiber, ArgumentsDescriptor descriptor, Object[] args,
192+
@Cached FiberTransferNode fiberTransferNode,
193193
@Cached InlinedBranchProfile errorProfile) {
194194

195195
final RubyFiber currentFiber = getLanguage().getCurrentFiber();
@@ -232,10 +232,10 @@ protected Object resume(
232232
public abstract static class FiberRaiseNode extends PrimitiveArrayArgumentsNode {
233233

234234
@Child private FiberResumeNode fiberResumeNode;
235-
@Child private FiberTransferNode fiberTransferNode;
236235

237236
@Specialization
238237
protected Object raise(RubyFiber fiber, RubyException exception,
238+
@Cached FiberTransferNode fiberTransferNode,
239239
@Cached InlinedBranchProfile errorProfile) {
240240
if (fiber.resumingFiber != null) {
241241
errorProfile.enter(this);
@@ -251,7 +251,7 @@ protected Object raise(RubyFiber fiber, RubyException exception,
251251

252252
if (fiber.status == FiberStatus.SUSPENDED && !fiber.yielding) {
253253
final RubyFiber currentFiber = getLanguage().getCurrentFiber();
254-
return getTransferNode().execute(
254+
return fiberTransferNode.execute(
255255
currentFiber,
256256
fiber,
257257
FiberOperation.RAISE,
@@ -271,14 +271,6 @@ private FiberResumeNode getResumeNode() {
271271
return fiberResumeNode;
272272
}
273273

274-
private FiberTransferNode getTransferNode() {
275-
if (fiberTransferNode == null) {
276-
CompilerDirectives.transferToInterpreterAndInvalidate();
277-
fiberTransferNode = insert(FiberTransferNode.create());
278-
}
279-
return fiberTransferNode;
280-
}
281-
282274
}
283275

284276
@CoreMethod(names = "resume", rest = true)
@@ -297,10 +289,10 @@ protected Object resume(VirtualFrame frame, RubyFiber fiber, Object[] rawArgs) {
297289
@CoreMethod(names = "yield", onSingleton = true, rest = true)
298290
public abstract static class YieldNode extends CoreMethodArrayArgumentsNode {
299291

300-
@Child private FiberTransferNode fiberTransferNode = FiberTransferNodeFactory.create(null);
301292

302293
@Specialization
303294
protected Object fiberYield(VirtualFrame frame, Object[] rawArgs,
295+
@Cached FiberTransferNode fiberTransferNode,
304296
@Cached InlinedBranchProfile errorProfile) {
305297

306298
final RubyFiber currentFiber = getLanguage().getCurrentFiber();

0 commit comments

Comments
 (0)