8787import com .oracle .graal .python .builtins .objects .cext .capi .transitions .CApiTransitions ;
8888import com .oracle .graal .python .builtins .objects .cext .capi .transitions .CApiTransitions .HandleContext ;
8989import com .oracle .graal .python .builtins .objects .cext .capi .transitions .CApiTransitions .NativeToPythonNode ;
90- import com .oracle .graal .python .builtins .objects .cext .capi .transitions .CApiTransitions .ToPythonWrapperNode ;
9190import com .oracle .graal .python .builtins .objects .cext .common .CExtCommonNodes ;
9291import com .oracle .graal .python .builtins .objects .cext .common .CExtCommonNodes .EnsureExecutableNode ;
9392import com .oracle .graal .python .builtins .objects .cext .common .CExtContext ;
9493import com .oracle .graal .python .builtins .objects .cext .common .LoadCExtException .ApiInitException ;
9594import com .oracle .graal .python .builtins .objects .cext .common .LoadCExtException .ImportException ;
9695import com .oracle .graal .python .builtins .objects .cext .common .NativePointer ;
9796import com .oracle .graal .python .builtins .objects .cext .copying .NativeLibraryLocator ;
98- import com .oracle .graal .python .builtins .objects .cext .structs .CConstants ;
9997import com .oracle .graal .python .builtins .objects .cext .structs .CFields ;
10098import com .oracle .graal .python .builtins .objects .cext .structs .CStructAccess ;
10199import com .oracle .graal .python .builtins .objects .cext .structs .CStructAccess .FreeNode ;
102- import com .oracle .graal .python .builtins .objects .cext .structs .CStructAccess .ReadPointerNode ;
103100import com .oracle .graal .python .builtins .objects .cext .structs .CStructs ;
104101import com .oracle .graal .python .builtins .objects .dict .PDict ;
105102import com .oracle .graal .python .builtins .objects .frame .PFrame ;
@@ -160,12 +157,6 @@ public final class CApiContext extends CExtContext {
160157
161158 public static final String LOGGER_CAPI_NAME = "capi" ;
162159
163- /** Same as _PY_NSMALLNEGINTS */
164- public static final int PY_NSMALLNEGINTS = 5 ;
165-
166- /** Same as _PY_NSMALLPOSINTS */
167- public static final int PY_NSMALLPOSINTS = 257 ;
168-
169160 /**
170161 * NFI source for Python module init functions (i.e. {@code "PyInit_modname"}).
171162 */
@@ -177,23 +168,6 @@ public final class CApiContext extends CExtContext {
177168 /** Native wrappers for context-insensitive singletons like {@link PNone#NONE}. */
178169 @ CompilationFinal (dimensions = 1 ) private final PythonAbstractObjectNativeWrapper [] singletonNativePtrs ;
179170
180- /**
181- * This cache is used to cache native wrappers for frequently used primitives. This is strictly
182- * defined to be the range {@code [-5, 256]}. CPython does exactly the same (see
183- * {@code PyLong_FromLong}; implemented in macro {@code CHECK_SMALL_INT}).
184- */
185- @ CompilationFinal (dimensions = 1 ) private final PrimitiveNativeWrapper [] primitiveNativeWrapperCache ;
186-
187- /**
188- * Pointer to a native array of long objects in interval
189- * [{@link com.oracle.graal.python.builtins.objects.cext.structs.CConstants#_PY_NSMALLNEGINTS
190- * -_PY_NSMALLNEGINTS},
191- * {@link com.oracle.graal.python.builtins.objects.cext.structs.CConstants#_PY_NSMALLPOSINTS
192- * _PY_NSMALLPOSINTS}[. This corresponds to CPython's {@code PyInterpreterState.small_ints} and
193- * is actually a native mirror of {@link #primitiveNativeWrapperCache}.
194- */
195- private Object nativeSmallIntsArray ;
196-
197171 /**
198172 * Pointer to the native {@code GCState GC state}. This corresponds to CPython's
199173 * {@code PyInterpreterState.gc}.
@@ -386,14 +360,6 @@ public CApiContext(PythonContext context, Object library, NativeLibraryLocator l
386360 singletonNativePtrs [i ] = new PythonObjectNativeWrapper (CONTEXT_INSENSITIVE_SINGLETONS [i ]);
387361 }
388362
389- // initialize primitive native wrapper cache
390- primitiveNativeWrapperCache = new PrimitiveNativeWrapper [PY_NSMALLNEGINTS + PY_NSMALLPOSINTS ];
391- for (int i = 0 ; i < primitiveNativeWrapperCache .length ; i ++) {
392- int value = i - PY_NSMALLNEGINTS ;
393- assert CApiGuards .isSmallInteger (value );
394- primitiveNativeWrapperCache [i ] = PrimitiveNativeWrapper .createInt (value );
395- }
396-
397363 // initialize Py_True and Py_False
398364 context .getTrue ().setNativeWrapper (PrimitiveNativeWrapper .createBool (true ));
399365 context .getFalse ().setNativeWrapper (PrimitiveNativeWrapper .createBool (false ));
@@ -500,89 +466,12 @@ private void freeSingletonNativeWrappers(HandleContext handleContext) {
500466 }
501467 }
502468
503- public PrimitiveNativeWrapper getCachedPrimitiveNativeWrapper (int i ) {
504- assert CApiGuards .isSmallInteger (i );
505- PrimitiveNativeWrapper primitiveNativeWrapper = primitiveNativeWrapperCache [i + 5 ];
506- assert primitiveNativeWrapper .getRefCount () > 0 ;
507- return primitiveNativeWrapper ;
508- }
509-
510- public PrimitiveNativeWrapper getCachedPrimitiveNativeWrapper (long l ) {
511- assert CApiGuards .isSmallLong (l );
512- return getCachedPrimitiveNativeWrapper ((int ) l );
513- }
514-
515469 public PrimitiveNativeWrapper getCachedBooleanPrimitiveNativeWrapper (boolean b ) {
516470 PythonAbstractObjectNativeWrapper wrapper = b ? getContext ().getTrue ().getNativeWrapper () : getContext ().getFalse ().getNativeWrapper ();
517471 assert wrapper .getRefCount () > 0 ;
518472 return (PrimitiveNativeWrapper ) wrapper ;
519473 }
520474
521- /**
522- * Returns or allocates (on demand) the native array {@code PyInterpreterState.small_ints} and
523- * write all elements to it.
524- */
525- Object getOrCreateSmallInts () {
526- CompilerAsserts .neverPartOfCompilation ();
527- // TODO(fa): this should not require the GIL (GR-51314)
528- assert getContext ().ownsGil ();
529- if (nativeSmallIntsArray == null ) {
530- assert CConstants ._PY_NSMALLNEGINTS .intValue () == PY_NSMALLNEGINTS ;
531- assert CConstants ._PY_NSMALLPOSINTS .intValue () == PY_NSMALLPOSINTS ;
532- Object smallInts = CStructAccess .AllocateNode .callocUncached (PY_NSMALLNEGINTS + PY_NSMALLPOSINTS , CStructAccess .POINTER_SIZE );
533- for (int i = 0 ; i < PY_NSMALLNEGINTS + PY_NSMALLPOSINTS ; i ++) {
534- CStructAccess .WritePointerNode .writeUncached (smallInts , i , CApiTransitions .HandlePointerConverter .intToPointer (i - PY_NSMALLNEGINTS ));
535- }
536- nativeSmallIntsArray = smallInts ;
537- }
538- return nativeSmallIntsArray ;
539- }
540-
541- /**
542- * Deallocates the native small int array (pointer {@link #nativeSmallIntsArray}) and all
543- * wrappers of the small ints (in {@link #primitiveNativeWrapperCache}) which are immortal and
544- * must therefore be explicitly free'd. This method modifies the
545- * {@link HandleContext#nativeStubLookup stub lookup table} but runs not guest code.
546- */
547- private void freeSmallInts (HandleContext handleContext ) {
548- CompilerAsserts .neverPartOfCompilation ();
549- // TODO(fa): this should not require the GIL (GR-51314)
550- assert getContext ().ownsGil ();
551- if (nativeSmallIntsArray != null ) {
552- assert verifyNativeSmallInts ();
553- // free the native array used to store the stub pointers of the small int wrappers
554- FreeNode .executeUncached (nativeSmallIntsArray );
555- nativeSmallIntsArray = null ;
556- }
557- for (PrimitiveNativeWrapper wrapper : primitiveNativeWrapperCache ) {
558- assert wrapper .isIntLike () && CApiGuards .isSmallLong (wrapper .getLong ());
559- assert !wrapper .isNative () || wrapper .getRefCount () == IMMORTAL_REFCNT ;
560- if (wrapper .ref != null ) {
561- CApiTransitions .nativeStubLookupRemove (handleContext , wrapper .ref );
562- }
563- }
564- }
565-
566- /**
567- * Verifies integrity of the pointers stored in the native small int array. Each pointer must
568- * denote the according small int wrapper. The objects are expected to be immortal.
569- */
570- private boolean verifyNativeSmallInts () {
571- // TODO(fa): this should not require the GIL (GR-51314)
572- assert getContext ().ownsGil ();
573- for (int i = 0 ; i < PY_NSMALLNEGINTS + PY_NSMALLPOSINTS ; i ++) {
574- Object elementPtr = ReadPointerNode .getUncached ().readArrayElement (nativeSmallIntsArray , i );
575- PythonNativeWrapper wrapper = ToPythonWrapperNode .executeUncached (elementPtr , false );
576- if (wrapper != primitiveNativeWrapperCache [i ]) {
577- return false ;
578- }
579- if (primitiveNativeWrapperCache [i ].isNative () && primitiveNativeWrapperCache [i ].getRefCount () != IMMORTAL_REFCNT ) {
580- return false ;
581- }
582- }
583- return true ;
584- }
585-
586475 /**
587476 * Allocates the {@code GCState} which needs to happen very early in the C API initialization
588477 * phase. <it>Very early</it> means it needs to happen before the first object (that takes part
@@ -1227,7 +1116,6 @@ public void finalizeCApi() {
12271116 try {
12281117 // TODO(fa): remove GIL acquisition (GR-51314)
12291118 try (GilNode .UncachedAcquire ignored = GilNode .uncachedAcquire ()) {
1230- freeSmallInts (handleContext );
12311119 freeSingletonNativeWrappers (handleContext );
12321120 /*
12331121 * Clear all remaining native object stubs. This must be done after the small int
0 commit comments