Skip to content

Commit ccb40a1

Browse files
committed
[GR-20381] Update SingletonClassNode to @GenerateUncached
PullRequest: truffleruby/2056
2 parents 339ea9c + e2df36d commit ccb40a1

File tree

3 files changed

+53
-54
lines changed

3 files changed

+53
-54
lines changed

src/main/java/org/truffleruby/builtins/CoreMethodNodeManager.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,14 @@
5454
public class CoreMethodNodeManager {
5555

5656
private final RubyContext context;
57-
private final SingletonClassNode singletonClassNode;
5857
private final PrimitiveManager primitiveManager;
5958
private final RubyLanguage language;
6059

6160
public CoreMethodNodeManager(
6261
RubyContext context,
63-
SingletonClassNode singletonClassNode,
6462
PrimitiveManager primitiveManager) {
6563
this.context = context;
6664
this.language = context.getLanguageSlow();
67-
this.singletonClassNode = singletonClassNode;
6865
this.primitiveManager = primitiveManager;
6966
}
7067

@@ -128,7 +125,7 @@ private RubyModule getModule(String fullName, boolean isClass) {
128125
}
129126

130127
private RubyClass getSingletonClass(Object object) {
131-
return singletonClassNode.executeSingletonClass(object);
128+
return SingletonClassNode.getUncached().executeSingletonClass(object);
132129
}
133130

134131
private void addCoreMethod(RubyModule module, MethodDetails methodDetails) {

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import org.truffleruby.extra.ffi.Pointer;
5151
import org.truffleruby.language.Nil;
5252
import org.truffleruby.language.NotProvided;
53-
import org.truffleruby.language.RubyContextNode;
5453
import org.truffleruby.language.RubyDynamicObject;
5554
import org.truffleruby.language.RubyGuards;
5655
import org.truffleruby.language.RubyRootNode;
@@ -294,33 +293,14 @@ private enum State {
294293

295294
private State state = State.INITIALIZING;
296295

297-
private static class CoreLibraryNode extends RubyContextNode {
298-
299-
@Child SingletonClassNode singletonClassNode;
300-
301-
public CoreLibraryNode() {
302-
this.singletonClassNode = SingletonClassNode.create();
303-
adoptChildren();
304-
}
305-
306-
public SingletonClassNode getSingletonClassNode() {
307-
return singletonClassNode;
308-
}
309-
310-
public RubyClass getSingletonClass(Object object) {
311-
return singletonClassNode.executeSingletonClass(object);
312-
}
313-
314-
}
315-
316-
private final CoreLibraryNode node;
296+
private final SingletonClassNode node;
317297

318298
public CoreLibrary(RubyContext context) {
319299
this.context = context;
320300
this.coreLoadPath = buildCoreLoadPath();
321301
this.corePath = coreLoadPath + File.separator + "core" + File.separator;
322302
this.sourceSection = initCoreSourceSection(context);
323-
this.node = new CoreLibraryNode();
303+
this.node = SingletonClassNode.getUncached();
324304

325305
final RubyLanguage language = context.getLanguageSlow();
326306

@@ -660,14 +640,13 @@ public void initialize() {
660640
public void loadCoreNodes(PrimitiveManager primitiveManager) {
661641
final CoreMethodNodeManager coreMethodNodeManager = new CoreMethodNodeManager(
662642
context,
663-
node.getSingletonClassNode(),
664643
primitiveManager);
665644

666645
coreMethodNodeManager.loadCoreMethodNodes();
667646

668647
basicObjectSendInfo = getMethod(basicObjectClass, "__send__").getSharedMethodInfo();
669648
kernelPublicSendInfo = getMethod(kernelModule, "public_send").getSharedMethodInfo();
670-
truffleBootMainInfo = getMethod(node.getSingletonClass(truffleBootModule), "main").getSharedMethodInfo();
649+
truffleBootMainInfo = getMethod(node.executeSingletonClass(truffleBootModule), "main").getSharedMethodInfo();
671650
}
672651

673652
private InternalMethod getMethod(RubyModule module, String name) {

src/main/java/org/truffleruby/language/objects/SingletonClassNode.java

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@
99
*/
1010
package org.truffleruby.language.objects;
1111

12+
import com.oracle.truffle.api.dsl.CachedContext;
13+
import com.oracle.truffle.api.dsl.GenerateUncached;
14+
import org.truffleruby.RubyContext;
15+
import org.truffleruby.RubyLanguage;
1216
import org.truffleruby.core.basicobject.BasicObjectNodes.ObjectIDNode;
1317
import org.truffleruby.core.klass.ClassNodes;
1418
import org.truffleruby.core.klass.RubyClass;
1519
import org.truffleruby.core.numeric.RubyBignum;
1620
import org.truffleruby.core.string.StringUtils;
1721
import org.truffleruby.core.symbol.RubySymbol;
1822
import org.truffleruby.language.Nil;
19-
import org.truffleruby.language.RubyContextSourceNode;
2023
import org.truffleruby.language.RubyDynamicObject;
2124
import org.truffleruby.language.RubyNode;
25+
import org.truffleruby.language.RubySourceNode;
2226
import org.truffleruby.language.control.RaiseException;
2327
import org.truffleruby.language.library.RubyLibrary;
2428
import org.truffleruby.language.objects.shared.SharedObjects;
@@ -28,8 +32,13 @@
2832
import com.oracle.truffle.api.dsl.NodeChild;
2933
import com.oracle.truffle.api.dsl.Specialization;
3034

35+
@GenerateUncached
3136
@NodeChild(value = "value", type = RubyNode.class)
32-
public abstract class SingletonClassNode extends RubyContextSourceNode {
37+
public abstract class SingletonClassNode extends RubySourceNode {
38+
39+
public static SingletonClassNode getUncached() {
40+
return SingletonClassNodeGen.getUncached();
41+
}
3342

3443
public static SingletonClassNode create() {
3544
return SingletonClassNodeGen.create(null);
@@ -38,43 +47,51 @@ public static SingletonClassNode create() {
3847
public abstract RubyClass executeSingletonClass(Object value);
3948

4049
@Specialization(guards = "value")
41-
protected RubyClass singletonClassTrue(boolean value) {
42-
return coreLibrary().trueClass;
50+
protected RubyClass singletonClassTrue(boolean value,
51+
@CachedContext(RubyLanguage.class) RubyContext context) {
52+
return context.getCoreLibrary().trueClass;
4353
}
4454

4555
@Specialization(guards = "!value")
46-
protected RubyClass singletonClassFalse(boolean value) {
47-
return coreLibrary().falseClass;
56+
protected RubyClass singletonClassFalse(boolean value,
57+
@CachedContext(RubyLanguage.class) RubyContext context) {
58+
return context.getCoreLibrary().falseClass;
4859
}
4960

5061
@Specialization
51-
protected RubyClass singletonClassNil(Nil value) {
52-
return coreLibrary().nilClass;
62+
protected RubyClass singletonClassNil(Nil value,
63+
@CachedContext(RubyLanguage.class) RubyContext context) {
64+
return context.getCoreLibrary().nilClass;
5365
}
5466

5567
@Specialization
56-
protected RubyClass singletonClass(int value) {
57-
return noSingletonClass();
68+
protected RubyClass singletonClass(int value,
69+
@CachedContext(RubyLanguage.class) RubyContext context) {
70+
return noSingletonClass(context);
5871
}
5972

6073
@Specialization
61-
protected RubyClass singletonClass(long value) {
62-
return noSingletonClass();
74+
protected RubyClass singletonClass(long value,
75+
@CachedContext(RubyLanguage.class) RubyContext context) {
76+
return noSingletonClass(context);
6377
}
6478

6579
@Specialization
66-
protected RubyClass singletonClass(double value) {
67-
return noSingletonClass();
80+
protected RubyClass singletonClass(double value,
81+
@CachedContext(RubyLanguage.class) RubyContext context) {
82+
return noSingletonClass(context);
6883
}
6984

7085
@Specialization
71-
protected RubyClass singletonClassBignum(RubyBignum value) {
72-
return noSingletonClass();
86+
protected RubyClass singletonClassBignum(RubyBignum value,
87+
@CachedContext(RubyLanguage.class) RubyContext context) {
88+
return noSingletonClass(context);
7389
}
7490

7591
@Specialization
76-
protected RubyClass singletonClassSymbol(RubySymbol value) {
77-
return noSingletonClass();
92+
protected RubyClass singletonClassSymbol(RubySymbol value,
93+
@CachedContext(RubyLanguage.class) RubyContext context) {
94+
return noSingletonClass(context);
7895
}
7996

8097
@Specialization(
@@ -87,8 +104,9 @@ protected RubyClass singletonClassClassCached(RubyClass rubyClass,
87104
}
88105

89106
@Specialization(replaces = "singletonClassClassCached")
90-
protected RubyClass singletonClassClassUncached(RubyClass rubyClass) {
91-
return ClassNodes.getSingletonClass(getContext(), rubyClass);
107+
protected RubyClass singletonClassClassUncached(RubyClass rubyClass,
108+
@CachedContext(RubyLanguage.class) RubyContext context) {
109+
return ClassNodes.getSingletonClass(context, rubyClass);
92110
}
93111

94112
@Specialization(
@@ -105,12 +123,12 @@ protected RubyClass singletonClassInstanceUncached(RubyDynamicObject object) {
105123
return getSingletonClassForInstance(object);
106124
}
107125

108-
private RubyClass noSingletonClass() {
109-
throw new RaiseException(getContext(), coreExceptions().typeErrorCantDefineSingleton(this));
126+
private RubyClass noSingletonClass(RubyContext context) {
127+
throw new RaiseException(context, context.getCoreExceptions().typeErrorCantDefineSingleton(this));
110128
}
111129

112130
protected RubyClass getSingletonClassOrNull(RubyClass rubyClass) {
113-
return ClassNodes.getSingletonClassOrNull(getContext(), rubyClass);
131+
return ClassNodes.getSingletonClassOrNull(rubyClass.fields.getContext(), rubyClass);
114132
}
115133

116134
@TruffleBoundary
@@ -120,6 +138,7 @@ protected RubyClass getSingletonClassForInstance(RubyDynamicObject object) {
120138
if (metaClass.isSingleton) {
121139
return metaClass;
122140
}
141+
final RubyContext context = metaClass.fields.getContext();
123142

124143
final RubyClass logicalClass = object.getLogicalClass();
125144

@@ -129,7 +148,7 @@ protected RubyClass getSingletonClassForInstance(RubyDynamicObject object) {
129148
ObjectIDNode.getUncached().execute(object));
130149

131150
final RubyClass singletonClass = ClassNodes.createSingletonClassOfObject(
132-
getContext(),
151+
context,
133152
getEncapsulatingSourceSection(),
134153
logicalClass,
135154
object,
@@ -139,15 +158,19 @@ protected RubyClass getSingletonClassForInstance(RubyDynamicObject object) {
139158
RubyLibrary.getUncached().freeze(singletonClass);
140159
}
141160

142-
SharedObjects.propagate(getContext(), object, singletonClass);
161+
SharedObjects.propagate(context, object, singletonClass);
143162
object.setMetaClass(singletonClass);
144163

145164
return singletonClass;
146165
}
147166
}
148167

149168
protected int getCacheLimit() {
150-
return getContext().getOptions().CLASS_CACHE;
169+
return RubyLanguage.getCurrentContext().getOptions().CLASS_CACHE;
170+
}
171+
172+
protected int getIdentityCacheLimit() {
173+
return RubyLanguage.getCurrentContext().getOptions().IDENTITY_CACHE;
151174
}
152175

153176
}

0 commit comments

Comments
 (0)