5656import com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .PromoteBorrowedValue ;
5757import com .oracle .graal .python .builtins .objects .PNone ;
5858import com .oracle .graal .python .builtins .objects .cext .PythonAbstractNativeObject ;
59+ import com .oracle .graal .python .builtins .objects .cext .structs .CStructAccess ;
5960import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes ;
6061import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes .GetItemNode ;
6162import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes .GetItemScalarNode ;
6263import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes .ListGeneralizationNode ;
6364import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes .SetItemScalarNode ;
64- import com .oracle .graal .python .builtins .objects .ints .PInt ;
6565import com .oracle .graal .python .builtins .objects .tuple .PTuple ;
6666import com .oracle .graal .python .lib .PySliceNew ;
6767import com .oracle .graal .python .lib .PyTupleSizeNode ;
6868import com .oracle .graal .python .nodes .ErrorMessages ;
6969import com .oracle .graal .python .nodes .PRaiseNode ;
7070import com .oracle .graal .python .nodes .builtins .TupleNodes .GetNativeTupleStorage ;
7171import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
72+ import com .oracle .graal .python .runtime .sequence .storage .NativeObjectSequenceStorage ;
7273import com .oracle .graal .python .runtime .sequence .storage .SequenceStorage ;
73- import com .oracle .graal .python .util .PythonUtils ;
7474import com .oracle .truffle .api .dsl .Bind ;
7575import com .oracle .truffle .api .dsl .Cached ;
7676import com .oracle .truffle .api .dsl .Cached .Shared ;
@@ -84,22 +84,22 @@ public final class PythonCextTupleBuiltins {
8484 abstract static class PyTuple_New extends CApiUnaryBuiltinNode {
8585
8686 @ Specialization
87- static PTuple doGeneric (long size ,
87+ static PTuple doGeneric (long longSize ,
8888 @ Bind ("this" ) Node inliningTarget ,
8989 @ Cached PythonObjectFactory factory ,
90- @ Cached PRaiseNode .Lazy raiseNode ) {
91- if (!PInt .isIntRange (size )) {
90+ @ Cached PRaiseNode .Lazy raiseNode ,
91+ @ Cached CStructAccess .AllocateNode alloc ) {
92+ int size = (int ) longSize ;
93+ if (longSize != size ) {
9294 throw raiseNode .get (inliningTarget ).raise (PythonBuiltinClassType .MemoryError );
9395 }
94- Object [] data = new Object [(int ) size ];
9596 /*
96- * We need to fill the empty object array with 'PNone.NO_VALUE' because it may be that
97- * the tuple is accessed with 'PyTuple_GET_ITEM' before all elements are initialized and
98- * the corresponding storage-to-native transition would then fail because of the Java
99- * nulls.
97+ * Already allocate the tuple with native memory, since it has to be populated from the
98+ * native side
10099 */
101- PythonUtils .fill (data , 0 , data .length , PNone .NO_VALUE );
102- return factory .createTuple (data );
100+ Object mem = alloc .alloc ((longSize + 1 ) * CStructAccess .POINTER_SIZE );
101+ NativeObjectSequenceStorage storage = NativeObjectSequenceStorage .create (mem , size , size , true );
102+ return factory .createTuple (storage );
103103 }
104104 }
105105
0 commit comments