Skip to content

Commit 55e97cb

Browse files
committed
use lazy classes in more places
1 parent c49b8c0 commit 55e97cb

33 files changed

+157
-146
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/PythonBuiltinClassType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public Shape getInstanceShape() {
212212
// set the base classes (and check uniqueness):
213213

214214
HashSet<String> set = new HashSet<>();
215-
for (PythonBuiltinClassType type : values()) {
215+
for (PythonBuiltinClassType type : VALUES) {
216216
assert set.add(type.name) : type.name();
217217
type.base = PythonObject;
218218
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
import com.oracle.graal.python.builtins.objects.set.SetNodes;
102102
import com.oracle.graal.python.builtins.objects.str.PString;
103103
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
104+
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
104105
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
105106
import com.oracle.graal.python.builtins.objects.type.PythonClass;
106107
import com.oracle.graal.python.nodes.PGuards;
@@ -125,6 +126,7 @@
125126
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
126127
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
127128
import com.oracle.graal.python.nodes.object.GetClassNode;
129+
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
128130
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
129131
import com.oracle.graal.python.nodes.subscript.SliceLiteralNode;
130132
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
@@ -529,14 +531,14 @@ public PythonObject reversed(PythonClass cls, String value) {
529531

530532
@Specialization(guards = {"!isString(sequence)", "!isPRange(sequence)"})
531533
public Object reversed(PythonClass cls, Object sequence,
532-
@Cached("create()") GetClassNode getClassNode,
534+
@Cached("create()") GetLazyClassNode getClassNode,
533535
@Cached("create(__REVERSED__)") LookupAttributeInMRONode reversedNode,
534536
@Cached("create()") CallUnaryMethodNode callReversedNode,
535537
@Cached("create(__LEN__)") LookupAndCallUnaryNode lenNode,
536538
@Cached("create(__GETITEM__)") LookupAttributeInMRONode getItemNode,
537539
@Cached("createBinaryProfile()") ConditionProfile noReversedProfile,
538540
@Cached("createBinaryProfile()") ConditionProfile noGetItemProfile) {
539-
PythonClass sequenceKlass = getClassNode.execute(sequence);
541+
LazyPythonClass sequenceKlass = getClassNode.execute(sequence);
540542
Object reversed = reversedNode.execute(sequenceKlass);
541543
if (noReversedProfile.profile(reversed == PNone.NO_VALUE)) {
542544
Object getItem = getItemNode.execute(sequenceKlass);
@@ -1192,12 +1194,11 @@ public boolean boolN(Object cls, PNone arg) {
11921194

11931195
@Specialization
11941196
public boolean bool(Object cls, Object obj,
1195-
@Cached("create(__BOOL__)") LookupAndCallUnaryNode callNode,
1196-
@Cached("create()") GetClassNode getClass) {
1197+
@Cached("create(__BOOL__)") LookupAndCallUnaryNode callNode) {
11971198
try {
11981199
return callNode.executeBoolean(obj);
11991200
} catch (UnexpectedResultException ex) {
1200-
throw raise(PythonErrorType.TypeError, "__bool__ should return bool, returned %s", getClass.execute(ex.getResult()));
1201+
throw raise(PythonErrorType.TypeError, "__bool__ should return bool, returned %p", ex.getResult());
12011202
}
12021203
}
12031204
}
@@ -1209,10 +1210,8 @@ public abstract static class ListNode extends PythonBinaryBuiltinNode {
12091210

12101211
@Specialization
12111212
protected PList constructList(PythonClass cls, Object value,
1212-
@Cached("create()") ConstructListNode constructListNode,
1213-
@Cached("create()") GetClassNode getClassNode) {
1214-
PythonClass valueClass = getClassNode.execute(value);
1215-
return constructListNode.execute(cls, value, valueClass);
1213+
@Cached("create()") ConstructListNode constructListNode) {
1214+
return constructListNode.execute(cls, value);
12161215
}
12171216

12181217
@Fallback

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SysModuleBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public Object run(
198198
} else {
199199
PBaseException exception = currentException.getExceptionObject();
200200
exception.reifyException();
201-
return factory().createTuple(new Object[]{getPythonClass(exception, getClassProfile), exception, exception.getTraceback(factory())});
201+
return factory().createTuple(new Object[]{getPythonClass(exception.getLazyPythonClass(), getClassProfile), exception, exception.getTraceback(factory())});
202202
}
203203
}
204204
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/TruffleCextBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ Object run(Object errorMarker,
366366
PException currentException = getContext().getCurrentException();
367367
if (currentException != null) {
368368
currentException.getExceptionObject().reifyException();
369-
return getPythonClass(currentException.getExceptionObject(), getClassProfile);
369+
return getPythonClass(currentException.getExceptionObject().getLazyPythonClass(), getClassProfile);
370370
}
371371
return errorMarker;
372372
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cell/CellBuiltins.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@
5555
import com.oracle.graal.python.builtins.objects.PNone;
5656
import com.oracle.graal.python.builtins.objects.PNotImplemented;
5757
import com.oracle.graal.python.builtins.objects.cell.CellBuiltinsFactory.GetRefNodeGen;
58-
import com.oracle.graal.python.builtins.objects.type.PythonClass;
5958
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6059
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
61-
import com.oracle.graal.python.nodes.object.GetClassNode;
60+
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
6261
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6362
import com.oracle.truffle.api.dsl.Cached;
6463
import com.oracle.truffle.api.dsl.Fallback;
@@ -121,13 +120,12 @@ public abstract static class ReprNode extends PythonBuiltinNode {
121120
@TruffleBoundary
122121
public String repr(PCell self,
123122
@Cached("create()") GetRefNode getRef,
124-
@Cached("create()") GetClassNode getClassNode) {
123+
@Cached("create()") GetLazyClassNode getClassNode) {
125124
Object ref = getRef.execute(self);
126125
if (ref == null) {
127-
return String.format("<cell at %s: empty>", this.hashCode());
126+
return String.format("<cell at %s: empty>", self.hashCode());
128127
}
129-
PythonClass refClass = getClassNode.execute(ref);
130-
return String.format("<cell at %s: %s object at %s>", this.hashCode(), refClass, ref.hashCode());
128+
return String.format("<cell at %s: %s object at %s>", self.hashCode(), getClassNode.execute(ref).getName(), ref.hashCode());
131129
}
132130

133131
@Fallback

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/CExtNodes.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
9999
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
100100
import com.oracle.graal.python.nodes.object.GetClassNode;
101+
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
101102
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
102103
import com.oracle.graal.python.nodes.util.CastToIndexNode;
103104
import com.oracle.graal.python.runtime.exception.PException;
@@ -407,7 +408,7 @@ Object doNativeWrapperGeneric(PythonNativeWrapper object) {
407408

408409
@Specialization(guards = {"isForeignObject(object, getClassNode, isForeignClassProfile)", "!isNativeWrapper(object)", "!isNativeNull(object)"}, limit = "1")
409410
PythonAbstractObject doNativeObject(TruffleObject object,
410-
@SuppressWarnings("unused") @Cached("create()") GetClassNode getClassNode,
411+
@SuppressWarnings("unused") @Cached("create()") GetLazyClassNode getClassNode,
411412
@SuppressWarnings("unused") @Cached("create()") IsBuiltinClassProfile isForeignClassProfile) {
412413
return factory().createNativeObjectWrapper(object);
413414
}
@@ -461,7 +462,7 @@ protected static boolean isPrimitiveNativeWrapper(PythonNativeWrapper object) {
461462
return object instanceof PrimitiveNativeWrapper && !isMaterialized((PrimitiveNativeWrapper) object) || object instanceof BoolNativeWrapper;
462463
}
463464

464-
protected boolean isForeignObject(TruffleObject obj, GetClassNode getClassNode, IsBuiltinClassProfile isForeignClassProfile) {
465+
protected boolean isForeignObject(TruffleObject obj, GetLazyClassNode getClassNode, IsBuiltinClassProfile isForeignClassProfile) {
465466
return isForeignClassProfile.profileClass(getClassNode.execute(obj), PythonBuiltinClassType.TruffleObject);
466467
}
467468

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/HashingStorageNodes.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import java.util.ArrayList;
5050
import java.util.Arrays;
5151

52-
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5352
import com.oracle.graal.python.builtins.objects.PNone;
5453
import com.oracle.graal.python.builtins.objects.common.DynamicObjectStorage.FastDictStorage;
5554
import com.oracle.graal.python.builtins.objects.common.DynamicObjectStorage.PythonObjectDictStorage;
@@ -73,7 +72,7 @@
7372
import com.oracle.graal.python.builtins.objects.ints.PInt;
7473
import com.oracle.graal.python.builtins.objects.object.PythonObject;
7574
import com.oracle.graal.python.builtins.objects.str.PString;
76-
import com.oracle.graal.python.builtins.objects.type.PythonClass;
75+
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
7776
import com.oracle.graal.python.nodes.PGuards;
7877
import com.oracle.graal.python.nodes.PNodeWithContext;
7978
import com.oracle.graal.python.nodes.SpecialMethodNames;
@@ -86,7 +85,7 @@
8685
import com.oracle.graal.python.nodes.datamodel.IsHashableNode;
8786
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
8887
import com.oracle.graal.python.nodes.expression.CastToBooleanNode;
89-
import com.oracle.graal.python.nodes.object.GetClassNode;
88+
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
9089
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
9190
import com.oracle.graal.python.runtime.exception.PException;
9291
import com.oracle.graal.python.runtime.exception.PythonErrorType;
@@ -178,7 +177,7 @@ public static PythonEquivalence create() {
178177

179178
@ImportStatic(PGuards.class)
180179
abstract static class DictStorageBaseNode extends PNodeWithContext {
181-
@Child private GetClassNode getClassNode;
180+
@Child private GetLazyClassNode getClassNode;
182181
@Child private IsHashableNode isHashableNode;
183182
@Child private Equivalence equivalenceNode;
184183

@@ -190,10 +189,10 @@ protected Equivalence getEquivalence() {
190189
return equivalenceNode;
191190
}
192191

193-
protected PythonClass getClass(Object object) {
192+
protected LazyPythonClass getClass(Object object) {
194193
if (getClassNode == null) {
195194
CompilerDirectives.transferToInterpreterAndInvalidate();
196-
getClassNode = insert(GetClassNode.create());
195+
getClassNode = insert(GetLazyClassNode.create());
197196
}
198197
return getClassNode.execute(object);
199198
}
@@ -224,7 +223,7 @@ protected PException unhashable(Object key) {
224223
}
225224

226225
protected boolean wrappedString(PString s) {
227-
return getClass(s).isBuiltin();
226+
return PGuards.cannotBeOverridden(getClass(s));
228227
}
229228

230229
protected EconomicMapStorage switchToEconomicMap(HashingStorage storage) {
@@ -366,14 +365,13 @@ public HashingStorage doSequence(PythonObject iterable, @SuppressWarnings("unuse
366365
Object it = getIterator.executeWith(iterable);
367366

368367
ArrayList<PSequence> elements = new ArrayList<>();
369-
PythonClass listClass = getCore().lookupType(PythonBuiltinClassType.PList);
370368
boolean isStringKey = false;
371369
try {
372370
while (true) {
373371
Object next = getNextNode().execute(it);
374372
PSequence element = null;
375373
int len = 1;
376-
element = createListNode.execute(listClass, next, getClass(next));
374+
element = createListNode.execute(next);
377375
assert element != null;
378376
// This constructs a new list using the builtin type. So, the object cannot
379377
// be subclassed and we can directly call 'len()'.

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ void doStorage(SequenceStorage s, SliceInfo info, PSequence seq,
949949
void doGeneric(SequenceStorage s, SliceInfo info, Object iterable,
950950
@Cached("create()") SetStorageSliceNode setStorageSliceNode,
951951
@Cached("create()") ListNodes.ConstructListNode constructListNode) {
952-
PList list = constructListNode.execute(iterable, null);
952+
PList list = constructListNode.execute(iterable);
953953
setStorageSliceNode.execute(s, info, list.getSequenceStorage());
954954
}
955955

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/BaseExceptionBuiltins.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
4949
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
5050
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
51-
import com.oracle.graal.python.nodes.object.GetClassNode;
51+
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
5252
import com.oracle.graal.python.runtime.formatting.ErrorMessageFormatter;
5353
import com.oracle.truffle.api.CompilerDirectives;
5454
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -90,14 +90,14 @@ public Object repr(PBaseException self) {
9090
@GenerateNodeFactory
9191
public abstract static class ArgsNode extends PythonBuiltinNode {
9292

93-
@Child private GetClassNode getClassNode;
93+
@Child private GetLazyClassNode getClassNode;
9494

9595
private final ErrorMessageFormatter formatter = new ErrorMessageFormatter();
9696

97-
private GetClassNode getGetClassNode() {
97+
private GetLazyClassNode getGetClassNode() {
9898
if (getClassNode == null) {
9999
CompilerDirectives.transferToInterpreterAndInvalidate();
100-
getClassNode = insert(GetClassNode.create());
100+
getClassNode = insert(GetLazyClassNode.create());
101101
}
102102
return getClassNode;
103103
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/method/BuiltinMethodBuiltins.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@
4040
import com.oracle.graal.python.builtins.objects.module.PythonModule;
4141
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
4242
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
43-
import com.oracle.graal.python.nodes.object.GetClassNode;
4443
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
4544
import com.oracle.graal.python.runtime.exception.PException;
4645
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
47-
import com.oracle.truffle.api.dsl.Cached;
4846
import com.oracle.truffle.api.dsl.Fallback;
4947
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
5048
import com.oracle.truffle.api.dsl.NodeFactory;
@@ -86,9 +84,8 @@ String reprBuiltinFunction(PBuiltinMethod self) {
8684

8785
@Specialization(guards = "!isBuiltinFunction(self)")
8886
@TruffleBoundary
89-
Object reprBuiltinMethod(PBuiltinMethod self,
90-
@Cached("create()") GetClassNode getClassNode) {
91-
return String.format("<built-in method %s of %s object at 0x%x>", self.getName(), getClassNode.execute(self.getSelf()).getName(), self.hashCode());
87+
Object reprBuiltinMethod(PBuiltinMethod self) {
88+
return String.format("<built-in method %s of %s object at 0x%x>", self.getName(), self.getSelf(), self.hashCode());
9289
}
9390
}
9491

0 commit comments

Comments
 (0)