Skip to content

Commit 4f7297c

Browse files
committed
No longer store the metaClass and logicalClass in the ObjectType
1 parent 7db4041 commit 4f7297c

File tree

9 files changed

+22
-132
lines changed

9 files changed

+22
-132
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ private RubyModule defineModule(SourceSection sourceSection, RubyModule lexicalP
911911
}
912912

913913
public static Shape createShape(Class<? extends RubyDynamicObject> layoutClass, RubyClass rubyClass) {
914-
final BasicObjectType objectType = new BasicObjectType(rubyClass, rubyClass);
914+
final BasicObjectType objectType = new BasicObjectType();
915915
return Shape.newBuilder().allowImplicitCastIntToLong(true).layout(layoutClass).dynamicType(objectType).build();
916916
}
917917

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import org.truffleruby.core.cast.ToRubyIntegerNode;
5353
import org.truffleruby.core.exception.RubyException;
5454
import org.truffleruby.core.fiber.FiberManager;
55-
import org.truffleruby.core.klass.ClassNodes;
5655
import org.truffleruby.core.klass.RubyClass;
5756
import org.truffleruby.core.method.RubyMethod;
5857
import org.truffleruby.core.module.RubyModule;
@@ -419,7 +418,7 @@ public abstract static class VMSetClassNode extends PrimitiveArrayArgumentsNode
419418
protected RubyDynamicObject setClass(RubyDynamicObject object, RubyClass newClass) {
420419
SharedObjects.propagate(getContext(), object, newClass);
421420
synchronized (object) {
422-
ClassNodes.setLogicalAndMetaClass(object, newClass);
421+
object.setMetaClass(newClass);
423422
}
424423
return object;
425424
}

src/main/java/org/truffleruby/core/basicobject/BasicObjectType.java

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,10 @@
1010
package org.truffleruby.core.basicobject;
1111

1212
import com.oracle.truffle.api.object.ObjectType;
13-
import com.oracle.truffle.api.object.Shape;
14-
import org.truffleruby.core.klass.RubyClass;
1513

1614
public final class BasicObjectType extends ObjectType {
1715

18-
private final RubyClass logicalClass;
19-
private final RubyClass metaClass;
20-
21-
public BasicObjectType(RubyClass logicalClass, RubyClass metaClass) {
22-
this.logicalClass = logicalClass;
23-
this.metaClass = metaClass;
24-
}
25-
26-
public RubyClass getLogicalClass() {
27-
assert logicalClass == metaClass.nonSingletonClass;
28-
return logicalClass;
29-
}
30-
31-
public BasicObjectType setLogicalClass(RubyClass logicalClass) {
32-
return new BasicObjectType(logicalClass, metaClass);
33-
}
34-
35-
public RubyClass getMetaClass() {
36-
assert metaClass.nonSingletonClass == logicalClass;
37-
return metaClass;
38-
}
39-
40-
public BasicObjectType setMetaClass(RubyClass metaClass) {
41-
return new BasicObjectType(logicalClass, metaClass);
42-
}
43-
44-
public static RubyClass getLogicalClass(Shape shape) {
45-
return ((BasicObjectType) shape.getObjectType()).getLogicalClass();
46-
}
47-
48-
public static RubyClass getMetaClass(Shape shape) {
49-
return ((BasicObjectType) shape.getObjectType()).getMetaClass();
16+
public BasicObjectType() {
5017
}
5118

5219
}

src/main/java/org/truffleruby/core/kernel/KernelNodes.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.truffleruby.core.array.RubyArray;
3939
import org.truffleruby.core.basicobject.BasicObjectNodes.ObjectIDNode;
4040
import org.truffleruby.core.basicobject.BasicObjectNodes.ReferenceEqualNode;
41-
import org.truffleruby.core.basicobject.BasicObjectType;
4241
import org.truffleruby.core.binding.BindingNodes;
4342
import org.truffleruby.core.binding.RubyBinding;
4443
import org.truffleruby.core.cast.BooleanCastNode;
@@ -443,7 +442,7 @@ protected RubyClass getClass(Object self) {
443442

444443
}
445444

446-
@ImportStatic({ ShapeCachingGuards.class, BasicObjectType.class })
445+
@ImportStatic(ShapeCachingGuards.class)
447446
public abstract static class CopyNode extends UnaryCoreMethodNode {
448447

449448
public static final Property[] EMPTY_PROPERTY_ARRAY = new Property[0];
@@ -460,10 +459,10 @@ public static CopyNode create() {
460459
@Specialization(guards = "self.getShape() == cachedShape", limit = "getCacheLimit()")
461460
protected RubyDynamicObject copyCached(RubyDynamicObject self,
462461
@Cached("self.getShape()") Shape cachedShape,
463-
@Cached("getLogicalClass(cachedShape)") RubyClass logicalClass,
464462
@Cached(value = "getCopiedProperties(cachedShape)", dimensions = 1) Property[] properties,
465463
@Cached("createWriteFieldNodes(properties)") DynamicObjectLibrary[] writeFieldNodes) {
466-
final RubyDynamicObject newObject = (RubyDynamicObject) allocateNode.call(logicalClass, "__allocate__");
464+
final RubyDynamicObject newObject = (RubyDynamicObject) allocateNode
465+
.call(self.getLogicalClass(), "__allocate__");
467466

468467
for (int i = 0; i < properties.length; i++) {
469468
final Property property = properties[i];

src/main/java/org/truffleruby/core/klass/ClassNodes.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
1515
import org.truffleruby.builtins.CoreModule;
1616
import org.truffleruby.core.CoreLibrary;
17-
import org.truffleruby.core.basicobject.BasicObjectType;
1817
import org.truffleruby.core.module.RubyModule;
1918
import org.truffleruby.core.proc.RubyProc;
2019
import org.truffleruby.core.string.StringUtils;
@@ -33,7 +32,6 @@
3332
import com.oracle.truffle.api.dsl.Cached;
3433
import com.oracle.truffle.api.dsl.Specialization;
3534
import com.oracle.truffle.api.frame.VirtualFrame;
36-
import com.oracle.truffle.api.object.DynamicObjectLibrary;
3735
import com.oracle.truffle.api.object.Shape;
3836
import com.oracle.truffle.api.profiles.BranchProfile;
3937
import com.oracle.truffle.api.source.SourceSection;
@@ -42,32 +40,12 @@
4240
@CoreModule(value = "Class", isClass = true)
4341
public abstract class ClassNodes {
4442

45-
@TruffleBoundary
46-
public static void setMetaClass(RubyDynamicObject object, RubyClass singletonClass) {
47-
final BasicObjectType objectType = (BasicObjectType) object.getShape().getObjectType();
48-
final BasicObjectType newObjectType = objectType.setMetaClass(singletonClass);
49-
DynamicObjectLibrary.getUncached().setDynamicType(object, newObjectType);
50-
51-
object.setMetaClass(singletonClass);
52-
}
53-
54-
@TruffleBoundary
55-
public static void setLogicalAndMetaClass(RubyDynamicObject object, RubyClass rubyClass) {
56-
final BasicObjectType objectType = (BasicObjectType) object.getShape().getObjectType();
57-
final BasicObjectType newObjectType = objectType.setLogicalClass(rubyClass).setMetaClass(rubyClass);
58-
DynamicObjectLibrary.getUncached().setDynamicType(object, newObjectType);
59-
60-
object.setMetaClass(rubyClass);
61-
}
62-
6343
/** Special constructor for class Class */
6444
@TruffleBoundary
6545
public static RubyClass createClassClass(RubyContext context) {
6646
final Shape tempShape = CoreLibrary.createShape(RubyClass.class, null);
6747
final RubyClass rubyClass = new RubyClass(context, tempShape);
6848

69-
setLogicalAndMetaClass(rubyClass, rubyClass);
70-
7149
assert rubyClass.getLogicalClass() == rubyClass;
7250
assert rubyClass.getMetaClass() == rubyClass;
7351

@@ -193,10 +171,7 @@ public static void initialize(RubyContext context, RubyClass rubyClass, RubyClas
193171

194172
public static void setInstanceShape(RubyClass rubyClass, RubyClass baseClass) {
195173
assert !rubyClass.isSingleton : "Singleton classes cannot be instantiated";
196-
final Shape parentShape = baseClass.instanceShape;
197-
final BasicObjectType objectType = (BasicObjectType) parentShape.getObjectType();
198-
final Shape newShape = parentShape.changeType(objectType.setLogicalClass(rubyClass).setMetaClass(rubyClass));
199-
rubyClass.instanceShape = newShape;
174+
rubyClass.instanceShape = baseClass.instanceShape;
200175
}
201176

202177
private static RubyClass ensureItHasSingletonClassCreated(RubyContext context, RubyClass rubyClass) {
@@ -251,7 +226,7 @@ private static RubyClass createSingletonClass(RubyContext context, RubyClass rub
251226
true,
252227
rubyClass);
253228
SharedObjects.propagate(context, rubyClass, metaClass);
254-
setMetaClass(rubyClass, metaClass);
229+
rubyClass.setMetaClass(metaClass);
255230

256231
return rubyClass.getMetaClass();
257232
}

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,18 @@
1111

1212
import org.truffleruby.RubyContext;
1313
import org.truffleruby.RubyLanguage;
14-
import org.truffleruby.core.basicobject.BasicObjectType;
1514
import org.truffleruby.core.klass.RubyClass;
1615
import org.truffleruby.core.numeric.RubyBignum;
1716
import org.truffleruby.core.symbol.RubySymbol;
1817
import org.truffleruby.language.Nil;
1918
import org.truffleruby.language.RubyBaseNode;
2019
import org.truffleruby.language.RubyDynamicObject;
2120

22-
import com.oracle.truffle.api.dsl.Cached;
2321
import com.oracle.truffle.api.dsl.CachedContext;
2422
import com.oracle.truffle.api.dsl.GenerateUncached;
25-
import com.oracle.truffle.api.dsl.ImportStatic;
2623
import com.oracle.truffle.api.dsl.Specialization;
27-
import com.oracle.truffle.api.object.Shape;
2824

2925
@GenerateUncached
30-
@ImportStatic({ ShapeCachingGuards.class, BasicObjectType.class })
3126
public abstract class LogicalClassNode extends RubyBaseNode {
3227

3328
public static LogicalClassNode create() {
@@ -84,23 +79,8 @@ protected RubyClass logicalClassSymbol(RubySymbol value,
8479
return context.getCoreLibrary().symbolClass;
8580
}
8681

87-
@Specialization(
88-
guards = "object.getShape() == cachedShape",
89-
assumptions = "cachedShape.getValidAssumption()",
90-
limit = "getCacheLimit()")
91-
protected RubyClass logicalClassCached(RubyDynamicObject object,
92-
@Cached("object.getShape()") Shape cachedShape,
93-
@Cached("getLogicalClass(cachedShape)") RubyClass logicalClass) {
94-
return logicalClass;
95-
}
96-
97-
@Specialization(guards = "updateShape(object)")
98-
protected RubyClass updateShapeAndLogicalClass(RubyDynamicObject object) {
99-
return executeLogicalClass(object);
100-
}
101-
102-
@Specialization(replaces = { "logicalClassCached", "updateShapeAndLogicalClass" })
103-
protected RubyClass logicalClassUncached(RubyDynamicObject object) {
82+
@Specialization
83+
protected RubyClass logicalClassObject(RubyDynamicObject object) {
10484
return object.getLogicalClass();
10585
}
10686

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

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import com.oracle.truffle.api.dsl.ReportPolymorphism;
1313
import org.truffleruby.RubyContext;
1414
import org.truffleruby.RubyLanguage;
15-
import org.truffleruby.core.basicobject.BasicObjectType;
1615
import org.truffleruby.core.klass.RubyClass;
1716
import org.truffleruby.core.numeric.RubyBignum;
1817
import org.truffleruby.core.symbol.RubySymbol;
@@ -23,13 +22,10 @@
2322
import com.oracle.truffle.api.dsl.Cached;
2423
import com.oracle.truffle.api.dsl.CachedContext;
2524
import com.oracle.truffle.api.dsl.GenerateUncached;
26-
import com.oracle.truffle.api.dsl.ImportStatic;
2725
import com.oracle.truffle.api.dsl.Specialization;
28-
import com.oracle.truffle.api.object.Shape;
2926

3027
@ReportPolymorphism
3128
@GenerateUncached
32-
@ImportStatic({ ShapeCachingGuards.class, BasicObjectType.class })
3329
public abstract class MetaClassNode extends RubyBaseNode {
3430

3531
public static MetaClassNode create() {
@@ -96,29 +92,11 @@ protected RubyClass metaClassSymbol(RubySymbol value,
9692
protected RubyClass singletonClassCached(RubyDynamicObject object,
9793
@Cached("object") RubyDynamicObject cachedObject,
9894
@Cached("object.getMetaClass()") RubyClass metaClass) {
99-
100-
return metaClass;
101-
}
102-
103-
@Specialization(
104-
guards = "object.getShape() == cachedShape",
105-
assumptions = "cachedShape.getValidAssumption()",
106-
replaces = "singletonClassCached",
107-
limit = "getCacheLimit()")
108-
protected RubyClass metaClassCached(RubyDynamicObject object,
109-
@Cached("object.getShape()") Shape cachedShape,
110-
@Cached("getMetaClass(cachedShape)") RubyClass metaClass) {
111-
11295
return metaClass;
11396
}
11497

115-
@Specialization(guards = "updateShape(object)")
116-
protected RubyClass updateShapeAndMetaClass(RubyDynamicObject object) {
117-
return execute(object);
118-
}
119-
120-
@Specialization(replaces = { "metaClassCached", "singletonClassCached", "updateShapeAndMetaClass" })
121-
protected RubyClass metaClassUncached(RubyDynamicObject object) {
98+
@Specialization(replaces = "singletonClassCached")
99+
protected RubyClass metaClassObject(RubyDynamicObject object) {
122100
return object.getMetaClass();
123101
}
124102

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.oracle.truffle.api.dsl.Cached;
2828
import com.oracle.truffle.api.dsl.NodeChild;
2929
import com.oracle.truffle.api.dsl.Specialization;
30-
import com.oracle.truffle.api.object.Shape;
3130

3231
@NodeChild(value = "value", type = RubyNode.class)
3332
public abstract class SingletonClassNode extends RubyContextSourceNode {
@@ -79,14 +78,11 @@ protected RubyClass singletonClassSymbol(RubySymbol value) {
7978
}
8079

8180
@Specialization(
82-
guards = {
83-
"rubyClass.getShape() == cachedShape",
84-
"cachedSingletonClass != null" },
85-
limit = "getCacheLimit()")
81+
guards = { "rubyClass == cachedClass", "cachedSingletonClass != null" },
82+
limit = "getIdentityCacheLimit()")
8683
protected RubyClass singletonClassClassCached(RubyClass rubyClass,
87-
@Cached("rubyClass.getShape()") Shape cachedShape,
88-
@Cached("getSingletonClassOrNull(rubyClass)") RubyClass cachedSingletonClass) {
89-
84+
@Cached("rubyClass") RubyClass cachedClass,
85+
@Cached("getSingletonClassOrNull(cachedClass)") RubyClass cachedSingletonClass) {
9086
return cachedSingletonClass;
9187
}
9288

@@ -149,8 +145,8 @@ protected RubyClass getSingletonClassForInstance(RubyDynamicObject object) {
149145
}
150146

151147
SharedObjects.propagate(getContext(), object, singletonClass);
148+
object.setMetaClass(singletonClass);
152149

153-
ClassNodes.setMetaClass(object, singletonClass);
154150
return singletonClass;
155151
}
156152
}

src/main/java/org/truffleruby/language/objects/shared/ShareObjectNode.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import java.util.ArrayList;
1313
import java.util.List;
1414

15-
import org.truffleruby.core.basicobject.BasicObjectType;
16-
import org.truffleruby.core.klass.RubyClass;
1715
import org.truffleruby.language.RubyContextNode;
1816
import org.truffleruby.language.RubyDynamicObject;
1917
import org.truffleruby.language.objects.ObjectGraph;
@@ -31,7 +29,7 @@
3129
import com.oracle.truffle.api.object.Shape;
3230

3331
/** Share the object and all that is reachable from it (see {@link ObjectGraph#getAdjacentObjects}) */
34-
@ImportStatic({ ShapeCachingGuards.class, BasicObjectType.class })
32+
@ImportStatic(ShapeCachingGuards.class)
3533
public abstract class ShareObjectNode extends RubyContextNode {
3634

3735
protected static final int CACHE_LIMIT = 8;
@@ -52,8 +50,6 @@ public ShareObjectNode(int depth) {
5250
protected void shareCached(RubyDynamicObject object,
5351
@Cached("object.getShape()") Shape cachedShape,
5452
@CachedLibrary(limit = "1") DynamicObjectLibrary objectLibrary,
55-
@Cached("getLogicalClass(cachedShape)") RubyClass logicalClass,
56-
@Cached("getMetaClass(cachedShape)") RubyClass metaClass,
5753
@Cached("createShareInternalFieldsNode()") ShareInternalFieldsNode shareInternalFieldsNode,
5854
@Cached("createReadAndShareFieldNodes(getObjectProperties(cachedShape))") ReadAndShareFieldNode[] readAndShareFieldNodes,
5955
@Cached("createSharedShape(cachedShape)") Shape sharedShape) {
@@ -63,18 +59,18 @@ protected void shareCached(RubyDynamicObject object,
6359
assert object.getShape() == sharedShape;
6460

6561
// Share the logical class
66-
if (!SharedObjects.isShared(getContext(), logicalClass.getShape())) {
62+
if (!SharedObjects.isShared(getContext(), object.getLogicalClass().getShape())) {
6763
// The logical class is fixed for a given Shape and only needs to be shared once
6864
CompilerDirectives.transferToInterpreterAndInvalidate();
69-
SharedObjects.writeBarrier(getContext(), logicalClass);
65+
SharedObjects.writeBarrier(getContext(), object.getLogicalClass());
7066
}
7167

7268
// Share the metaclass. Note that the metaclass might refer to `object` via `attached`,
7369
// so it is important to share the object first.
74-
if (!SharedObjects.isShared(getContext(), metaClass.getShape())) {
70+
if (!SharedObjects.isShared(getContext(), object.getMetaClass().getShape())) {
7571
// The metaclass is fixed for a given Shape and only needs to be shared once
7672
CompilerDirectives.transferToInterpreterAndInvalidate();
77-
SharedObjects.writeBarrier(getContext(), metaClass);
73+
SharedObjects.writeBarrier(getContext(), object.getMetaClass());
7874
}
7975

8076
shareInternalFieldsNode.executeShare(object);

0 commit comments

Comments
 (0)