7777import static com .oracle .graal .python .builtins .objects .cext .capi .transitions .ArgDescriptor .setter ;
7878import static com .oracle .graal .python .builtins .objects .cext .capi .transitions .ArgDescriptor .vectorcallfunc ;
7979import static com .oracle .graal .python .nodes .HiddenAttr .METHOD_DEF_PTR ;
80- import static com .oracle .graal .python .nodes .HiddenAttr .NATIVE_STORAGE ;
8180import static com .oracle .graal .python .nodes .HiddenAttr .PROMOTED_START ;
8281import static com .oracle .graal .python .nodes .HiddenAttr .PROMOTED_STEP ;
8382import static com .oracle .graal .python .nodes .HiddenAttr .PROMOTED_STOP ;
111110import com .oracle .graal .python .builtins .objects .object .PythonObject ;
112111import com .oracle .graal .python .builtins .objects .set .PBaseSet ;
113112import com .oracle .graal .python .builtins .objects .slice .PSlice ;
114- import com .oracle .graal .python .builtins .objects .str .NativeCharSequence ;
113+ import com .oracle .graal .python .builtins .objects .str .NativeStringData ;
115114import com .oracle .graal .python .builtins .objects .str .PString ;
116115import com .oracle .graal .python .builtins .objects .str .StringNodes ;
117116import com .oracle .graal .python .builtins .objects .str .StringNodes .StringLenNode ;
@@ -167,10 +166,12 @@ abstract static class GraalPyPrivate_Get_PyASCIIObject_state_ascii extends CApiU
167166 int get (PString object ,
168167 @ Bind Node inliningTarget ,
169168 @ Cached InlinedConditionProfile storageProfile ,
169+ @ Cached HiddenAttr .ReadNode readAttrNode ,
170170 @ Cached TruffleString .GetCodeRangeNode getCodeRangeNode ) {
171171 // important: avoid materialization of native sequences
172- if (storageProfile .profile (inliningTarget , object .isNativeCharSequence ())) {
173- return object .getNativeCharSequence ().isAsciiOnly () ? 1 : 0 ;
172+ NativeStringData nativeData = object .getNativeStringData (inliningTarget , readAttrNode );
173+ if (storageProfile .profile (inliningTarget , nativeData != null )) {
174+ return nativeData .isAscii () ? 1 : 0 ;
174175 }
175176
176177 TruffleString string = object .getMaterialized ();
@@ -205,10 +206,12 @@ abstract static class GraalPyPrivate_Get_PyASCIIObject_state_kind extends CApiUn
205206 static int get (PString object ,
206207 @ Bind Node inliningTarget ,
207208 @ Cached InlinedConditionProfile storageProfile ,
209+ @ Cached HiddenAttr .ReadNode readAttrNode ,
208210 @ Cached TruffleString .GetCodeRangeNode getCodeRangeNode ) {
209211 // important: avoid materialization of native sequences
210- if (storageProfile .profile (inliningTarget , object .isNativeCharSequence ())) {
211- return object .getNativeCharSequence ().getElementSize () & 0b111;
212+ NativeStringData nativeData = object .getNativeStringData (inliningTarget , readAttrNode );
213+ if (storageProfile .profile (inliningTarget , nativeData != null )) {
214+ return nativeData .getCharSize ();
212215 }
213216 TruffleString string = object .getMaterialized ();
214217 TruffleString .CodeRange range = getCodeRangeNode .execute (string , TS_ENCODING );
@@ -677,10 +680,12 @@ static Object get(PString object,
677680 @ Cached TruffleString .SwitchEncodingNode switchEncodingNode ,
678681 @ Cached CStructAccess .AllocateNode allocateNode ,
679682 @ Cached CStructAccess .WriteTruffleStringNode writeTruffleStringNode ,
680- @ Cached HiddenAttr .WriteNode writeAttribute ) {
681- if (object .isNativeCharSequence ()) {
683+ @ Cached HiddenAttr .ReadNode readAttrNode ,
684+ @ Cached HiddenAttr .WriteNode writeAttrNode ) {
685+ NativeStringData nativeData = object .getNativeStringData (inliningTarget , readAttrNode );
686+ if (nativeData != null ) {
682687 // in this case, we can just return the pointer
683- return object . getNativeCharSequence () .getPtr ();
688+ return nativeData .getPtr ();
684689 }
685690 TruffleString string = object .getMaterialized ();
686691 TruffleString .CodeRange range = getCodeRangeNode .execute (string , TS_ENCODING );
@@ -702,20 +707,14 @@ static Object get(PString object,
702707 encoding = TruffleString .Encoding .UTF_32 ;
703708 }
704709 string = switchEncodingNode .execute (string , encoding );
705- int byteLength = string .byteLength (encoding ) + /* null terminator */ charSize ;
706- Object ptr = allocateNode .alloc (byteLength );
710+ int byteLength = string .byteLength (encoding );
711+ Object ptr = allocateNode .alloc (byteLength + /* null terminator */ charSize );
707712 writeTruffleStringNode .write (ptr , string , encoding );
708713 /*
709- * Set native char sequence , so we can just return the pointer the next time.
714+ * Set native data , so we can just return the pointer the next time.
710715 */
711- NativeCharSequence nativeSequence = new NativeCharSequence (ptr , string .byteLength (encoding ) / charSize , charSize , isAscii );
712- object .setNativeCharSequence (nativeSequence );
713- /*
714- * Create a native sequence storage to manage the lifetime of the native memory.
715- *
716- * TODO it would be nicer if the native char sequence could manage its own memory
717- */
718- writeAttribute .execute (inliningTarget , object , NATIVE_STORAGE , NativeByteSequenceStorage .create (ptr , byteLength , byteLength , true ));
716+ NativeStringData data = NativeStringData .create (charSize , isAscii , ptr , byteLength );
717+ object .setNativeStringData (inliningTarget , writeAttrNode , data );
719718 return ptr ;
720719 }
721720 }
0 commit comments