Skip to content

Commit f4171cf

Browse files
committed
Use manage capi context to store interned dict
1 parent b6db481 commit f4171cf

File tree

5 files changed

+33
-47
lines changed

5 files changed

+33
-47
lines changed

graalpython/com.oracle.graal.python.cext/src/capi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ typedef struct {
347347
BUILTIN(PyTruffleUnicode_Decode, PyObject*, PyObject*, const char*, const char*) \
348348
BUILTIN(PyTruffleUnicode_DecodeUTF8Stateful, PyObject*, void*, const char*, int) \
349349
BUILTIN(PyTruffleUnicode_FromUCS, PyObject*, void*, Py_ssize_t, int) \
350-
BUILTIN(PyTruffleUnicode_LookupAndIntern, PyObject*, PyObject*, PyObject*) \
350+
BUILTIN(PyTruffleUnicode_LookupAndIntern, PyObject*, PyObject*) \
351351
BUILTIN(PyTruffleUnicode_New, PyObject*, void*, Py_ssize_t, Py_UCS4) \
352352
BUILTIN(PyTruffle_Arg_ParseTupleAndKeywords, int, PyObject*, PyObject*, const char*, void*, void*) \
353353
BUILTIN(PyTruffle_ByteArray_EmptyWithCapacity, PyObject*, Py_ssize_t) \

graalpython/com.oracle.graal.python.cext/src/unicodeobject.c

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -226,36 +226,12 @@ static PyObject *interned = NULL;
226226

227227
void PyUnicode_InternInPlace(PyObject **p) {
228228
PyObject *s = *p;
229-
if (s == NULL || !PyUnicode_Check(s)) {
230-
return;
231-
}
232-
233-
/* If it's a subclass, we don't really know what putting
234-
it in the interned dict might do. */
235-
if (!PyUnicode_CheckExact(s)) {
229+
if (s == NULL) {
236230
return;
237231
}
238232

239-
// will be checked by `GraalPyTruffleUnicode_LookupAndIntern` upcall
240-
// if (PyUnicode_CHECK_INTERNED(s)) {
241-
// return;
242-
// }
243-
244-
// if (PyUnicode_READY(s) == -1) {
245-
// PyErr_Clear();
246-
// return;
247-
// }
248-
249-
if (interned == NULL) {
250-
interned = PyDict_New();
251-
if (interned == NULL) {
252-
PyErr_Clear(); /* Don't leave an exception */
253-
return;
254-
}
255-
}
256-
257233
// PyObject *t = PyDict_SetDefault(interned, s, s);
258-
PyObject *t = GraalPyTruffleUnicode_LookupAndIntern(interned, s);
234+
PyObject *t = GraalPyTruffleUnicode_LookupAndIntern(s);
259235
if (t == NULL) {
260236
PyErr_Clear();
261237
return;
@@ -266,14 +242,6 @@ void PyUnicode_InternInPlace(PyObject **p) {
266242
Py_SETREF(*p, t);
267243
return;
268244
}
269-
270-
/* The two references in interned dict (key and value) are not counted by
271-
refcnt. unicode_dealloc() and _PyUnicode_ClearInterned() take care of
272-
this. */
273-
// has been set already by `GraalPyTruffleUnicode_LookupAndIntern` upcall
274-
// Py_SET_REFCNT(s, Py_REFCNT(s) - 2);
275-
// _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL;
276-
277245
}
278246

279247
// taken from CPython "Python/Objects/unicodeobject.c"

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextBuiltinRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ private PythonCextBuiltinRegistry() {
303303
public static final CApiBuiltinExecutable PyTruffleUnicode_Decode = new CApiBuiltinExecutable("PyTruffleUnicode_Decode", CApiCallPath.Ignored, ArgDescriptor.PyObjectTransfer, new ArgDescriptor[]{ArgDescriptor.PyObject, ArgDescriptor.ConstCharPtrAsTruffleString, ArgDescriptor.ConstCharPtrAsTruffleString}, 242);
304304
public static final CApiBuiltinExecutable PyTruffleUnicode_DecodeUTF8Stateful = new CApiBuiltinExecutable("PyTruffleUnicode_DecodeUTF8Stateful", CApiCallPath.Ignored, ArgDescriptor.PyObjectTransfer, new ArgDescriptor[]{ArgDescriptor.Pointer, ArgDescriptor.ConstCharPtrAsTruffleString, ArgDescriptor.Int}, 243);
305305
public static final CApiBuiltinExecutable PyTruffleUnicode_FromUCS = new CApiBuiltinExecutable("PyTruffleUnicode_FromUCS", CApiCallPath.Ignored, ArgDescriptor.PyObjectTransfer, new ArgDescriptor[]{ArgDescriptor.Pointer, ArgDescriptor.Py_ssize_t, ArgDescriptor.Int}, 244);
306-
public static final CApiBuiltinExecutable PyTruffleUnicode_LookupAndIntern = new CApiBuiltinExecutable("PyTruffleUnicode_LookupAndIntern", CApiCallPath.Ignored, ArgDescriptor.PyObject, new ArgDescriptor[]{ArgDescriptor.PyObject, ArgDescriptor.PyObject}, 245);
306+
public static final CApiBuiltinExecutable PyTruffleUnicode_LookupAndIntern = new CApiBuiltinExecutable("PyTruffleUnicode_LookupAndIntern", CApiCallPath.Ignored, ArgDescriptor.PyObject, new ArgDescriptor[]{ArgDescriptor.PyObject}, 245);
307307
public static final CApiBuiltinExecutable PyTruffleUnicode_New = new CApiBuiltinExecutable("PyTruffleUnicode_New", CApiCallPath.Ignored, ArgDescriptor.PyObjectTransfer, new ArgDescriptor[]{ArgDescriptor.Pointer, ArgDescriptor.Py_ssize_t, ArgDescriptor.PY_UCS4}, 246);
308308
public static final CApiBuiltinExecutable PyTruffle_Arg_ParseTupleAndKeywords = new CApiBuiltinExecutable("PyTruffle_Arg_ParseTupleAndKeywords", CApiCallPath.Ignored, ArgDescriptor.Int, new ArgDescriptor[]{ArgDescriptor.PyObject, ArgDescriptor.PyObject, ArgDescriptor.ConstCharPtrAsTruffleString, ArgDescriptor.Pointer, ArgDescriptor.Pointer}, 247);
309309
public static final CApiBuiltinExecutable PyTruffle_ByteArray_EmptyWithCapacity = new CApiBuiltinExecutable("PyTruffle_ByteArray_EmptyWithCapacity", CApiCallPath.Ignored, ArgDescriptor.PyObjectTransfer, new ArgDescriptor[]{ArgDescriptor.Py_ssize_t}, 248);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextUnicodeBuiltins.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@
8484
import com.oracle.graal.python.builtins.modules.BuiltinFunctions.ChrNode;
8585
import com.oracle.graal.python.builtins.modules.CodecsModuleBuiltins;
8686
import com.oracle.graal.python.builtins.modules.CodecsModuleBuiltins.CodecsEncodeNode;
87-
import com.oracle.graal.python.builtins.modules.SysModuleBuiltins.InternNode;
8887
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApi5BuiltinNode;
8988
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApi6BuiltinNode;
9089
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiBinaryBuiltinNode;
@@ -124,6 +123,7 @@
124123
import com.oracle.graal.python.builtins.objects.str.StringBuiltins.RFindNode;
125124
import com.oracle.graal.python.builtins.objects.str.StringBuiltins.ReplaceNode;
126125
import com.oracle.graal.python.builtins.objects.str.StringBuiltins.StartsWithNode;
126+
import com.oracle.graal.python.builtins.objects.str.StringNodes;
127127
import com.oracle.graal.python.lib.PyObjectIsTrueNode;
128128
import com.oracle.graal.python.lib.PyObjectLookupAttr;
129129
import com.oracle.graal.python.lib.PySliceNew;
@@ -280,37 +280,45 @@ protected boolean isStringSubtype(Object obj, GetClassNode getClassNode, IsSubty
280280
}
281281
}
282282

283-
@CApiBuiltin(ret = PyObject, args = {PyObject, PyObject}, call = Ignored)
284-
abstract static class PyTruffleUnicode_LookupAndIntern extends CApiBinaryBuiltinNode {
283+
@CApiBuiltin(ret = PyObject, args = {PyObject}, call = Ignored)
284+
abstract static class PyTruffleUnicode_LookupAndIntern extends CApiUnaryBuiltinNode {
285285
@Specialization
286-
Object withTS(PDict dict, TruffleString str,
287-
@Shared @Cached InternNode internNode,
286+
Object withTS(TruffleString str,
287+
@Shared @Cached StringNodes.InternStringNode internNode,
288288
@Shared @Cached HashingStorageGetItem getItem,
289289
@Shared @Cached HashingStorageSetItem setItem) {
290+
PDict dict = getCApiContext().getInternedUnicode();
291+
if (dict == null) {
292+
dict = factory().createDict();
293+
getCApiContext().setInternedUnicode(dict);
294+
}
290295
Object interned = getItem.execute(dict.getDictStorage(), str);
291296
if (interned == null) {
292-
interned = internNode.execute(null, str);
297+
interned = internNode.execute(str);
293298
dict.setDictStorage(setItem.execute(dict.getDictStorage(), str, interned));
294299
}
295300
return interned;
296301
}
297302

298303
@Specialization
299-
Object withPString(PDict dict, PString str,
304+
Object withPString(PString str,
300305
@Cached ReadAttributeFromDynamicObjectNode readNode,
301-
@Shared @Cached InternNode internNode,
306+
@Shared @Cached StringNodes.InternStringNode internNode,
302307
@Shared @Cached HashingStorageGetItem getItem,
303308
@Shared @Cached HashingStorageSetItem setItem) {
304309
boolean isInterned = readNode.execute(str, PString.INTERNED) != PNone.NO_VALUE;
305310
if (isInterned) {
306311
return str;
307312
}
308-
return withTS(dict, str.getValueUncached(), internNode, getItem, setItem);
313+
return withTS(str.getValueUncached(), internNode, getItem, setItem);
309314
}
310315

311316
@Fallback
312-
Object nil(@SuppressWarnings("unused") Object dict,
313-
@SuppressWarnings("unused") Object obj) {
317+
Object nil(@SuppressWarnings("unused") Object obj) {
318+
/*
319+
* If it's a subclass, we don't really know what putting it in the interned dict might
320+
* do.
321+
*/
314322
return PNone.NONE;
315323
}
316324
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ public final class CApiContext extends CExtContext {
189189
/** Same as {@code import.c: extensions} but we don't keep a PDict; just a bare Java HashMap. */
190190
private final HashMap<Pair<TruffleString, TruffleString>, Object> extensions = new HashMap<>(4);
191191

192+
/** corresponds to {@code unicodeobject.c: interned} */
193+
private PDict internedUnicode;
192194
private final ArrayList<Object> modulesByIndex = new ArrayList<>(0);
193195

194196
public final HashMap<Long, PLock> locks = new HashMap<>();
@@ -276,6 +278,14 @@ public static Object asHex(Object ptr) {
276278
return Objects.toString(ptr);
277279
}
278280

281+
public PDict getInternedUnicode() {
282+
return internedUnicode;
283+
}
284+
285+
public void setInternedUnicode(PDict internedUnicode) {
286+
this.internedUnicode = internedUnicode;
287+
}
288+
279289
/**
280290
* Tries to convert the object to a pointer (type: {@code long}) to avoid materialization of
281291
* pointer objects. If that is not possible, the object will be returned as given.

0 commit comments

Comments
 (0)