5454import static com .oracle .graal .python .builtins .objects .cext .capi .transitions .ArgDescriptor .PyObjectTransfer ;
5555import static com .oracle .graal .python .builtins .objects .cext .capi .transitions .ArgDescriptor .Py_hash_t ;
5656import static com .oracle .graal .python .builtins .objects .cext .capi .transitions .ArgDescriptor .Void ;
57- import static com .oracle .graal .python .runtime .nativeaccess .NativeMemory .NULLPTR ;
58- import static com .oracle .graal .python .runtime .nativeaccess .NativeMemory .readLong ;
59- import static com .oracle .graal .python .runtime .nativeaccess .NativeMemory .writeLong ;
6057import static com .oracle .graal .python .nodes .ErrorMessages .BAD_ARG_TO_INTERNAL_FUNC_WAS_S_P ;
6158import static com .oracle .graal .python .nodes .ErrorMessages .HASH_MISMATCH ;
6259import static com .oracle .graal .python .nodes .ErrorMessages .OBJ_P_HAS_NO_ATTR_S ;
6360import static com .oracle .graal .python .nodes .SpecialMethodNames .T_KEYS ;
6461import static com .oracle .graal .python .nodes .SpecialMethodNames .T_UPDATE ;
6562import static com .oracle .graal .python .runtime .PythonContext .NATIVE_NULL ;
63+ import static com .oracle .graal .python .runtime .nativeaccess .NativeMemory .NULLPTR ;
64+ import static com .oracle .graal .python .runtime .nativeaccess .NativeMemory .readLong ;
65+ import static com .oracle .graal .python .runtime .nativeaccess .NativeMemory .writeLong ;
6666
6767import java .util .logging .Level ;
6868
7171import com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CApi5BuiltinNode ;
7272import com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CApiBinaryBuiltinNode ;
7373import com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CApiBuiltin ;
74- import com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CApiNullaryBuiltinNode ;
7574import com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CApiQuaternaryBuiltinNode ;
7675import com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CApiTernaryBuiltinNode ;
7776import com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CApiUnaryBuiltinNode ;
78- import com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .PromoteBorrowedValue ;
7977import com .oracle .graal .python .builtins .objects .PNone ;
8078import com .oracle .graal .python .builtins .objects .cext .capi .CApiContext ;
79+ import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .EnsurePythonObjectNode ;
80+ import com .oracle .graal .python .builtins .objects .cext .capi .transitions .CApiTiming ;
8181import com .oracle .graal .python .builtins .objects .cext .capi .transitions .CApiTransitions ;
8282import com .oracle .graal .python .builtins .objects .cext .capi .transitions .CApiTransitions .GcNativePtrToPythonNode ;
8383import com .oracle .graal .python .builtins .objects .cext .capi .transitions .CApiTransitions .HandlePointerConverter ;
84+ import com .oracle .graal .python .builtins .objects .cext .capi .transitions .CApiTransitions .PythonToNativeInternalNode ;
8485import com .oracle .graal .python .builtins .objects .common .DynamicObjectStorage ;
8586import com .oracle .graal .python .builtins .objects .common .EconomicMapStorage ;
8687import com .oracle .graal .python .builtins .objects .common .HashingCollectionNodes .SetItemNode ;
114115import com .oracle .graal .python .lib .PyObjectGetAttr ;
115116import com .oracle .graal .python .lib .PyObjectHashNode ;
116117import com .oracle .graal .python .lib .PyUnicodeCheckNode ;
117- import com .oracle .graal .python .runtime .nativeaccess .NativeMemory ;
118118import com .oracle .graal .python .nodes .PRaiseNode ;
119119import com .oracle .graal .python .nodes .builtins .ListNodes .ConstructListNode ;
120120import com .oracle .graal .python .nodes .call .CallNode ;
121121import com .oracle .graal .python .nodes .util .CastToJavaLongExactNode ;
122+ import com .oracle .graal .python .runtime .PythonContext ;
122123import com .oracle .graal .python .runtime .exception .PException ;
124+ import com .oracle .graal .python .runtime .nativeaccess .NativeMemory ;
123125import com .oracle .graal .python .runtime .object .PFactory ;
124126import com .oracle .graal .python .runtime .sequence .storage .SequenceStorage ;
125127import com .oracle .graal .python .util .PythonUtils ;
143145public final class PythonCextDictBuiltins {
144146 private static final TruffleLogger LOGGER = CApiContext .getLogger (PythonCextDictBuiltins .class );
145147
146- @ CApiBuiltin (ret = PyObjectTransfer , args = {}, call = Direct )
147- abstract static class PyDict_New extends CApiNullaryBuiltinNode {
148+ private static final CApiTiming TIMING_PYDICT_NEW = CApiTiming .create (false , "PyDict_New" );
148149
149- @ Specialization
150- static Object run (
151- @ Bind PythonLanguage language ) {
152- return PFactory .createDict (language );
150+ @ CApiBuiltin (ret = PyObjectTransfer , args = {}, call = Direct , acquireGil = false )
151+ static long PyDict_New () {
152+ CApiTiming .enter ();
153+ try {
154+ PDict dict = PFactory .createDict (PythonLanguage .get (null ));
155+ return PythonToNativeInternalNode .executeUncached (dict , true );
156+ } finally {
157+ CApiTiming .exit (TIMING_PYDICT_NEW );
153158 }
154159 }
155160
@@ -159,6 +164,7 @@ abstract static class _PyDict_Next extends CApi5BuiltinNode {
159164 @ Specialization
160165 static int next (PDict dict , long posPtr , long keyPtr , long valuePtr , long hashPtr ,
161166 @ Bind Node inliningTarget ,
167+ @ Bind PythonContext context ,
162168 @ Cached CApiTransitions .PythonToNativeNode toNativeNode ,
163169 @ Cached InlinedBranchProfile needsRewriteProfile ,
164170 @ Cached InlinedBranchProfile economicMapProfile ,
@@ -168,8 +174,8 @@ static int next(PDict dict, long posPtr, long keyPtr, long valuePtr, long hashPt
168174 @ Cached HashingStorageIteratorKey itKey ,
169175 @ Cached HashingStorageIteratorValue itValue ,
170176 @ Cached HashingStorageIteratorKeyHash itKeyHash ,
171- @ Cached PromoteBorrowedValue promoteKeyNode ,
172- @ Cached PromoteBorrowedValue promoteValueNode ,
177+ @ Cached EnsurePythonObjectNode ensureKeyNode ,
178+ @ Cached EnsurePythonObjectNode ensureValueNode ,
173179 @ Cached HashingStorageSetItem setItem ,
174180 @ Cached DynamicObject .SetShapeFlagsNode setShapeFlagsNode ) {
175181 /*
@@ -188,8 +194,10 @@ static int next(PDict dict, long posPtr, long keyPtr, long valuePtr, long hashPt
188194 economicMapProfile .enter (inliningTarget );
189195 HashingStorageIterator it = getIterator .execute (inliningTarget , storage );
190196 while (itNext .execute (inliningTarget , storage , it )) {
191- if (promoteKeyNode .execute (inliningTarget , itKey .execute (inliningTarget , storage , it )) != null ||
192- promoteValueNode .execute (inliningTarget , itValue .execute (inliningTarget , storage , it )) != null ) {
197+ Object key = itKey .execute (inliningTarget , storage , it );
198+ Object value = itValue .execute (inliningTarget , storage , it );
199+ if (ensureKeyNode .execute (context , key , false ) != key ||
200+ ensureValueNode .execute (context , value , false ) != value ) {
193201 needsRewrite = true ;
194202 break ;
195203 }
@@ -208,12 +216,12 @@ static int next(PDict dict, long posPtr, long keyPtr, long valuePtr, long hashPt
208216 while (itNext .execute (inliningTarget , storage , it )) {
209217 Object key = itKey .execute (inliningTarget , storage , it );
210218 Object value = itValue .execute (inliningTarget , storage , it );
211- Object promotedKey = promoteKeyNode .execute (inliningTarget , key );
212- if (promotedKey != null ) {
219+ Object promotedKey = ensureKeyNode .execute (context , key , false );
220+ if (promotedKey != key ) {
213221 key = promotedKey ;
214222 }
215- Object promotedValue = promoteValueNode .execute (inliningTarget , value );
216- if (promotedValue != null ) {
223+ Object promotedValue = ensureValueNode .execute (context , value , false );
224+ if (promotedValue != value ) {
217225 value = promotedValue ;
218226 }
219227 // promoted key will never have side-effecting __hash__/__eq__
@@ -243,13 +251,13 @@ static int next(PDict dict, long posPtr, long keyPtr, long valuePtr, long hashPt
243251 writeLong (posPtr , newPos );
244252 if (keyPtr != NULLPTR ) {
245253 Object key = itKey .execute (inliningTarget , storage , it );
246- assert promoteKeyNode .execute (inliningTarget , key ) == null ;
254+ assert ensureKeyNode .execute (context , key , false ) == key ;
247255 // Borrowed reference
248256 NativeMemory .writePtr (keyPtr , toNativeNode .executeLong (key ));
249257 }
250258 if (valuePtr != NULLPTR ) {
251259 Object value = itValue .execute (inliningTarget , storage , it );
252- assert promoteValueNode .execute (inliningTarget , value ) == null ;
260+ assert ensureValueNode .execute (context , value , false ) == value ;
253261 // Borrowed reference
254262 NativeMemory .writePtr (valuePtr , toNativeNode .executeLong (value ));
255263 }
@@ -303,8 +311,9 @@ public abstract static class PyDict_GetItem extends CApiBinaryBuiltinNode {
303311 @ Specialization
304312 static Object getItem (PDict dict , Object key ,
305313 @ Bind Node inliningTarget ,
314+ @ Bind PythonContext context ,
306315 @ Cached HashingStorageGetItem getItem ,
307- @ Cached PromoteBorrowedValue promoteNode ,
316+ @ Cached EnsurePythonObjectNode ensureNode ,
308317 @ Cached SetItemNode setItemNode ,
309318 @ Cached InlinedBranchProfile noResultProfile ) {
310319 try {
@@ -313,8 +322,8 @@ static Object getItem(PDict dict, Object key,
313322 noResultProfile .enter (inliningTarget );
314323 return NATIVE_NULL ;
315324 }
316- Object promotedValue = promoteNode .execute (inliningTarget , res );
317- if (promotedValue != null ) {
325+ Object promotedValue = ensureNode .execute (context , res , false );
326+ if (promotedValue != res ) {
318327 setItemNode .execute (null , inliningTarget , dict , key , promotedValue );
319328 return promotedValue ;
320329 }
@@ -342,17 +351,18 @@ abstract static class PyDict_GetItemWithError extends CApiBinaryBuiltinNode {
342351 @ Specialization
343352 static Object getItem (PDict dict , Object key ,
344353 @ Bind Node inliningTarget ,
354+ @ Bind PythonContext context ,
345355 @ Cached HashingStorageGetItem getItem ,
346- @ Cached PromoteBorrowedValue promoteNode ,
356+ @ Cached EnsurePythonObjectNode ensureNode ,
347357 @ Cached SetItemNode setItemNode ,
348358 @ Cached InlinedBranchProfile noResultProfile ) {
349359 Object res = getItem .execute (null , inliningTarget , dict .getDictStorage (), key );
350360 if (res == null ) {
351361 noResultProfile .enter (inliningTarget );
352362 return NATIVE_NULL ;
353363 }
354- Object promotedValue = promoteNode .execute (inliningTarget , res );
355- if (promotedValue != null ) {
364+ Object promotedValue = ensureNode .execute (context , res , false );
365+ if (promotedValue != res ) {
356366 setItemNode .execute (null , inliningTarget , dict , key , promotedValue );
357367 return promotedValue ;
358368 }
@@ -412,12 +422,13 @@ abstract static class PyDict_SetDefault extends CApiTernaryBuiltinNode {
412422 @ Specialization
413423 static Object setItem (PDict dict , Object key , Object value ,
414424 @ Bind Node inliningTarget ,
425+ @ Bind PythonContext context ,
415426 @ Cached PyDictSetDefault setDefault ,
416- @ Cached PromoteBorrowedValue promoteNode ,
427+ @ Cached EnsurePythonObjectNode ensureNode ,
417428 @ Cached SetItemNode setItemNode ) {
418429 Object result = setDefault .execute (null , inliningTarget , dict , key , value );
419- Object promotedValue = promoteNode .execute (inliningTarget , result );
420- if (promotedValue != null ) {
430+ Object promotedValue = ensureNode .execute (context , result , false );
431+ if (promotedValue != result ) {
421432 setItemNode .execute (null , inliningTarget , dict , key , promotedValue );
422433 return promotedValue ;
423434 }
0 commit comments