Skip to content

Commit f9082f4

Browse files
committed
Clean up HashingStorageNodes usage and refactor
1 parent 7ae318e commit f9082f4

33 files changed

+1117
-1192
lines changed

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

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__SLOTS__;
6262
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__WEAKREF__;
6363
import static com.oracle.graal.python.nodes.SpecialMethodNames.DECODE;
64+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__COMPLEX__;
6465
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETITEM__;
6566
import static com.oracle.graal.python.runtime.exception.PythonErrorType.NotImplementedError;
6667
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SystemError;
@@ -95,9 +96,9 @@
9596
import com.oracle.graal.python.builtins.objects.code.CodeNodes;
9697
import com.oracle.graal.python.builtins.objects.code.PCode;
9798
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
98-
import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary;
99+
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
99100
import com.oracle.graal.python.builtins.objects.common.HashingStorage.DictEntry;
100-
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
101+
import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary;
101102
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
102103
import com.oracle.graal.python.builtins.objects.common.SequenceNodes.GetObjectArrayNode;
103104
import com.oracle.graal.python.builtins.objects.common.SequenceNodesFactory.GetObjectArrayNodeGen;
@@ -143,7 +144,6 @@
143144
import com.oracle.graal.python.nodes.PGuards;
144145
import com.oracle.graal.python.nodes.PRaiseNode;
145146
import com.oracle.graal.python.nodes.SpecialMethodNames;
146-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__COMPLEX__;
147147
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
148148
import com.oracle.graal.python.nodes.attributes.GetAttributeNode.GetAnyAttributeNode;
149149
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
@@ -2267,7 +2267,6 @@ public abstract static class TypeNode extends PythonBuiltinNode {
22672267
@Child private SequenceStorageNodes.LenNode slotLenNode;
22682268
@Child private SequenceStorageNodes.GetItemNode getItemNode;
22692269
@Child private SequenceStorageNodes.AppendNode appendNode;
2270-
@Child private HashingStorageNodes.ContainsKeyNode containsKeyNode;
22712270
@Child private CExtNodes.PCallCapiFunction callAddNativeSlotsNode;
22722271
@Child private CExtNodes.ToSulongNode toSulongNode;
22732272
@Child private ReadCallerFrameNode readCallerFrameNode;
@@ -2287,11 +2286,11 @@ Object type(Object cls, Object obj, PNone bases, PNone dict, PKeyword[] kwds,
22872286
@Specialization
22882287
Object type(VirtualFrame frame, LazyPythonClass cls, String name, PTuple bases, PDict namespace, PKeyword[] kwds,
22892288
@Cached GetLazyClassNode getMetaclassNode,
2290-
@CachedLibrary(limit = "1") HashingStorageLibrary hlib,
2289+
@CachedLibrary(limit = "1") HashingStorageLibrary nslib,
2290+
@CachedLibrary(limit = "1") HashingStorageLibrary glib,
2291+
@Cached BranchProfile updatedStorage,
22912292
@Cached("create(__NEW__)") LookupInheritedAttributeNode getNewFuncNode,
22922293
@Cached("create(__INIT_SUBCLASS__)") GetAttributeNode getInitSubclassNode,
2293-
@Cached HashingStorageNodes.GetItemNode getClasscellNode,
2294-
@Cached HashingStorageNodes.DelItemNode delClasscellNode,
22952294
@Cached CallNode callInitSubclassNode,
22962295
@Cached CallNode callNewFuncNode) {
22972296

@@ -2308,7 +2307,7 @@ Object type(VirtualFrame frame, LazyPythonClass cls, String name, PTuple bases,
23082307
}
23092308

23102309
try {
2311-
PythonClass newType = typeMetaclass(frame, name, bases, namespace, metaclass);
2310+
PythonClass newType = typeMetaclass(frame, name, bases, namespace, metaclass, nslib);
23122311

23132312
// TODO: Call __set_name__ on all descriptors in a newly generated type
23142313

@@ -2323,22 +2322,28 @@ Object type(VirtualFrame frame, LazyPythonClass cls, String name, PTuple bases,
23232322
PFrame callerFrame = getReadCallerFrameNode().executeWith(frame, 0);
23242323
PythonObject globals = callerFrame.getGlobals();
23252324
if (globals != null) {
2326-
String moduleName = getModuleNameFromGlobals(frame, globals, hlib);
2325+
String moduleName = getModuleNameFromGlobals(globals, glib);
23272326
if (moduleName != null) {
23282327
ensureWriteAttrNode().execute(frame, newType, __MODULE__, moduleName);
23292328
}
23302329
}
23312330
}
23322331

23332332
// set __class__ cell contents
2334-
Object classcell = getClasscellNode.execute(frame, namespace.getDictStorage(), __CLASSCELL__);
2333+
Object classcell = nslib.getItem(namespace.getDictStorage(), __CLASSCELL__);
23352334
if (classcell != null) {
23362335
if (classcell instanceof PCell) {
23372336
((PCell) classcell).setRef(newType);
23382337
} else {
23392338
raise(TypeError, "__classcell__ must be a cell");
23402339
}
2341-
delClasscellNode.execute(frame, namespace, namespace.getDictStorage(), __CLASSCELL__);
2340+
if (nslib.hasKey(namespace.getDictStorage(), __CLASSCELL__)) {
2341+
HashingStorage newStore = nslib.delItem(namespace.getDictStorage(), __CLASSCELL__);
2342+
if (newStore != namespace.getDictStorage()) {
2343+
updatedStorage.enter();
2344+
namespace.setDictStorage(newStore);
2345+
}
2346+
}
23422347
}
23432348

23442349
return newType;
@@ -2347,7 +2352,7 @@ Object type(VirtualFrame frame, LazyPythonClass cls, String name, PTuple bases,
23472352
}
23482353
}
23492354

2350-
private String getModuleNameFromGlobals(VirtualFrame frame, PythonObject globals, HashingStorageLibrary hlib) {
2355+
private String getModuleNameFromGlobals(PythonObject globals, HashingStorageLibrary hlib) {
23512356
Object nameAttr;
23522357
if (globals instanceof PythonModule) {
23532358
nameAttr = ensureReadAttrNode().execute(globals, __NAME__);
@@ -2361,7 +2366,7 @@ private String getModuleNameFromGlobals(VirtualFrame frame, PythonObject globals
23612366
}
23622367

23632368
@SuppressWarnings("try")
2364-
private PythonClass typeMetaclass(VirtualFrame frame, String name, PTuple bases, PDict namespace, LazyPythonClass metaclass) {
2369+
private PythonClass typeMetaclass(VirtualFrame frame, String name, PTuple bases, PDict namespace, LazyPythonClass metaclass, HashingStorageLibrary nslib) {
23652370

23662371
Object[] array = ensureGetObjectArrayNode().execute(bases);
23672372

@@ -2463,7 +2468,7 @@ private PythonClass typeMetaclass(VirtualFrame frame, String name, PTuple bases,
24632468
PythonContext context = getContextRef().get();
24642469
Object state = ForeignCallContext.enter(frame, context, this);
24652470
try {
2466-
PTuple newSlots = copySlots(name, slotList, slotlen, addDict, false, namespace);
2471+
PTuple newSlots = copySlots(name, slotList, slotlen, addDict, false, namespace, nslib);
24672472
pythonClass.setAttribute(__SLOTS__, newSlots);
24682473
if (basesArray.length > 1) {
24692474
// TODO: tfel - check if secondary bases provide weakref or dict when we
@@ -2483,7 +2488,7 @@ private PythonClass typeMetaclass(VirtualFrame frame, String name, PTuple bases,
24832488
}
24842489

24852490
@TruffleBoundary
2486-
private PTuple copySlots(String className, SequenceStorage slotList, int slotlen, boolean add_dict, boolean add_weak, PDict namespace) {
2491+
private PTuple copySlots(String className, SequenceStorage slotList, int slotlen, boolean add_dict, boolean add_weak, PDict namespace, HashingStorageLibrary nslib) {
24872492
SequenceStorage newSlots = new ObjectSequenceStorage(slotlen - PInt.intValue(add_dict) - PInt.intValue(add_weak));
24882493
int j = 0;
24892494
for (int i = 0; i < slotlen; i++) {
@@ -2503,7 +2508,7 @@ private PTuple copySlots(String className, SequenceStorage slotList, int slotlen
25032508
setSlotItemNode().execute(newSlots, slotName, NoGeneralizationNode.DEFAULT);
25042509
// Passing 'null' frame is fine because the caller already transfers the exception
25052510
// state to the context.
2506-
if (getContainsKeyNode().execute(null, namespace.getDictStorage(), slotName)) {
2511+
if (nslib.hasKey(namespace.getDictStorage(), slotName)) {
25072512
throw raise(PythonBuiltinClassType.ValueError, "%s in __slots__ conflicts with class variable", slotName);
25082513
}
25092514
j++;
@@ -2552,14 +2557,6 @@ private String mangle(String privateobj, String ident) {
25522557
return "_" + privateobj.substring(ipriv) + ident;
25532558
}
25542559

2555-
private HashingStorageNodes.ContainsKeyNode getContainsKeyNode() {
2556-
if (containsKeyNode == null) {
2557-
CompilerDirectives.transferToInterpreterAndInvalidate();
2558-
containsKeyNode = insert(HashingStorageNodes.ContainsKeyNode.create());
2559-
}
2560-
return containsKeyNode;
2561-
}
2562-
25632560
private SequenceStorageNodes.GetItemNode getSlotItemNode() {
25642561
if (getItemNode == null) {
25652562
CompilerDirectives.transferToInterpreterAndInvalidate();
@@ -2986,7 +2983,8 @@ Object call(VirtualFrame frame, LazyPythonClass cls, int argcount, int kwonlyarg
29862983
@CachedLibrary("codestring") PythonObjectLibrary codestringBufferLib,
29872984
@CachedLibrary("lnotab") PythonObjectLibrary lnotabBufferLib,
29882985
@Cached CodeNodes.CreateCodeNode createCodeNode,
2989-
@Cached GetObjectArrayNode getObjectArrayNode) throws UnsupportedMessageException {
2986+
@Cached GetObjectArrayNode getObjectArrayNode,
2987+
@CachedLibrary(limit = "1") HashingStorageLibrary lib) throws UnsupportedMessageException {
29902988
byte[] codeBytes = codestringBufferLib.getBufferBytes(codestring);
29912989
byte[] lnotabBytes = lnotabBufferLib.getBufferBytes(lnotab);
29922990

@@ -3001,7 +2999,7 @@ Object call(VirtualFrame frame, LazyPythonClass cls, int argcount, int kwonlyarg
30012999
codeBytes, constantsArr, namesArr,
30023000
varnamesArr, freevarsArr, cellcarsArr,
30033001
getStringArg(filename), getStringArg(name), firstlineno,
3004-
lnotabBytes);
3002+
lnotabBytes, lib);
30053003
}
30063004

30073005
@Specialization(guards = {"codestringBufferLib.isBuffer(codestring)", "lnotabBufferLib.isBuffer(lnotab)"}, limit = "2", rewriteOn = UnsupportedMessageException.class)
@@ -3089,7 +3087,7 @@ Object doMapping(LazyPythonClass klass, PHashingCollection obj) {
30893087

30903088
@Specialization(guards = {"isSequence(frame, obj, lib)", "!isBuiltinMapping(obj)"})
30913089
Object doMapping(VirtualFrame frame, LazyPythonClass klass, PythonObject obj,
3092-
@Cached("create()") HashingStorageNodes.InitNode initNode,
3090+
@Cached("create()") HashingStorage.InitNode initNode,
30933091
@SuppressWarnings("unused") @CachedLibrary(limit = "1") PythonObjectLibrary lib) {
30943092
return factory().createMappingproxy(klass, initNode.execute(frame, obj, PKeyword.EMPTY_KEYWORDS));
30953093
}

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -64,10 +64,12 @@
6464
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.AsPythonObjectNode;
6565
import com.oracle.graal.python.builtins.objects.code.PCode;
6666
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes.SetItemNode;
67-
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
67+
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
68+
import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary;
6869
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
6970
import com.oracle.graal.python.builtins.objects.dict.PDict;
7071
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
72+
import com.oracle.graal.python.builtins.objects.function.PArguments;
7173
import com.oracle.graal.python.builtins.objects.ints.PInt;
7274
import com.oracle.graal.python.builtins.objects.list.PList;
7375
import com.oracle.graal.python.builtins.objects.module.PythonModule;
@@ -112,6 +114,7 @@
112114
import com.oracle.truffle.api.interop.UnsupportedTypeException;
113115
import com.oracle.truffle.api.library.CachedLibrary;
114116
import com.oracle.truffle.api.nodes.LanguageInfo;
117+
import com.oracle.truffle.api.profiles.ConditionProfile;
115118
import com.oracle.truffle.api.source.Source;
116119
import com.oracle.truffle.api.source.Source.SourceBuilder;
117120
import com.oracle.truffle.llvm.api.Toolchain;
@@ -367,12 +370,24 @@ public Object run(PythonModule extensionModule) {
367370
@Builtin(name = "is_builtin", minNumOfPositionalArgs = 1)
368371
@GenerateNodeFactory
369372
public abstract static class IsBuiltin extends PythonBuiltinNode {
373+
374+
protected boolean isWithinContext() {
375+
return getContext() != null && getContext().isInitialized();
376+
}
377+
378+
protected HashingStorage getStorage() {
379+
return isWithinContext() ? getContext().getImportedModules().getDictStorage() : null;
380+
}
381+
370382
@Specialization
371383
public int run(VirtualFrame frame, String name,
372-
@Cached("create()") HashingStorageNodes.ContainsKeyNode hasKey) {
384+
@CachedLibrary(limit = "1") HashingStorageLibrary hasKeyLib,
385+
@Cached("createBinaryProfile()") ConditionProfile hasFrame) {
373386
if (getCore().lookupBuiltinModule(name) != null) {
374387
return 1;
375-
} else if (getContext() != null && getContext().isInitialized() && hasKey.execute(frame, getContext().getImportedModules().getDictStorage(), name)) {
388+
} else if (isWithinContext() && hasFrame.profile(frame != null) && hasKeyLib.hasKeyWithState(getStorage(), name, PArguments.getThreadState(frame))) {
389+
return -1;
390+
} else if (isWithinContext() && hasKeyLib.hasKey(getStorage(), name)) {
376391
return -1;
377392
} else {
378393
return 0;

0 commit comments

Comments
 (0)