|
58 | 58 | import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.ConstCharPtr;
|
59 | 59 | import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.ConstCharPtrAsTruffleString;
|
60 | 60 | import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.Int;
|
| 61 | +import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.PY_SSIZE_T_PTR; |
61 | 62 | import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.PY_UCS4;
|
62 | 63 | import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.PY_UNICODE_PTR;
|
63 | 64 | import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.Pointer;
|
@@ -1021,68 +1022,69 @@ public static _PyUnicode_AsUTF8String create() {
|
1021 | 1022 | }
|
1022 | 1023 | }
|
1023 | 1024 |
|
1024 |
| - @CApiBuiltin(ret = ConstCharPtr, args = {PyObject}, call = Direct) |
1025 |
| - abstract static class PyTruffle_Unicode_AsUTF8AndSize_CharPtr extends CApiUnaryBuiltinNode { |
| 1025 | + @CApiBuiltin(ret = ConstCharPtr, args = {PyObject, PY_SSIZE_T_PTR}, call = Direct) |
| 1026 | + abstract static class PyUnicode_AsUTF8AndSize extends CApiBinaryBuiltinNode { |
1026 | 1027 |
|
1027 | 1028 | @Specialization
|
1028 |
| - static Object doUnicode(PString s, |
| 1029 | + static Object doUnicode(PString s, Object sizePtr, |
1029 | 1030 | @Bind("this") Node inliningTarget,
|
1030 |
| - @Cached InlinedConditionProfile profile, |
| 1031 | + @Shared @CachedLibrary(limit = "2") InteropLibrary lib, |
| 1032 | + @Shared @Cached InlinedConditionProfile hasSizeProfile, |
| 1033 | + @Shared @Cached InlinedConditionProfile hasUtf8Profile, |
| 1034 | + @Shared @Cached CStructAccess.WriteLongNode writeLongNode, |
1031 | 1035 | @Shared @Cached _PyUnicode_AsUTF8String asUTF8String) {
|
1032 |
| - if (profile.profile(inliningTarget, s.getUtf8Bytes() == null)) { |
| 1036 | + if (hasUtf8Profile.profile(inliningTarget, s.getUtf8Bytes() == null)) { |
1033 | 1037 | PBytes bytes = (PBytes) asUTF8String.execute(s, T_STRICT);
|
1034 | 1038 | s.setUtf8Bytes(bytes);
|
1035 | 1039 | }
|
| 1040 | + if (hasSizeProfile.profile(inliningTarget, !lib.isNull(sizePtr))) { |
| 1041 | + writeLongNode.write(sizePtr, s.getUtf8Bytes().getSequenceStorage().length()); |
| 1042 | + } |
1036 | 1043 | return PySequenceArrayWrapper.ensureNativeSequence(s.getUtf8Bytes());
|
1037 | 1044 | }
|
1038 | 1045 |
|
1039 | 1046 | @Specialization
|
1040 |
| - static Object doNative(PythonAbstractNativeObject s, |
1041 |
| - @CachedLibrary(limit = "2") InteropLibrary lib, |
| 1047 | + static Object doNative(PythonAbstractNativeObject s, Object sizePtr, |
| 1048 | + @Bind("this") Node inliningTarget, |
| 1049 | + @Shared @CachedLibrary(limit = "2") InteropLibrary lib, |
| 1050 | + @Shared @Cached InlinedConditionProfile hasSizeProfile, |
| 1051 | + @Shared @Cached InlinedConditionProfile hasUtf8Profile, |
| 1052 | + @Shared @Cached CStructAccess.WriteLongNode writeLongNode, |
| 1053 | + @Shared @Cached _PyUnicode_AsUTF8String asUTF8String, |
1042 | 1054 | @CachedLibrary(limit = "1") PythonBufferAccessLibrary bufferLib,
|
1043 | 1055 | @Cached CStructAccess.ReadPointerNode readPointerNode,
|
1044 | 1056 | @Cached CStructAccess.WritePointerNode writePointerNode,
|
1045 | 1057 | @Cached CStructAccess.AllocateNode allocateNode,
|
1046 | 1058 | @Cached CStructAccess.WriteByteNode writeByteNode,
|
1047 |
| - @Cached CStructAccess.WriteLongNode writeLongNode, |
1048 |
| - @Shared @Cached _PyUnicode_AsUTF8String asUTF8String) { |
| 1059 | + @Cached CStructAccess.ReadI64Node readI64Node) { |
1049 | 1060 | Object utf8 = readPointerNode.readFromObj(s, CFields.PyCompactUnicodeObject__utf8);
|
1050 |
| - if (lib.isNull(utf8)) { |
| 1061 | + if (hasUtf8Profile.profile(inliningTarget, lib.isNull(utf8))) { |
1051 | 1062 | PBytes bytes = (PBytes) asUTF8String.execute(s, T_STRICT);
|
1052 | 1063 | int len = bufferLib.getBufferLength(bytes);
|
1053 | 1064 | Object mem = allocateNode.alloc(len + 1, true);
|
1054 |
| - writeByteNode.writeByteArray(mem, bufferLib.getInternalByteArray(bytes), len, 0, 0); |
| 1065 | + writeByteNode.writeByteArray(mem, bufferLib.getInternalOrCopiedByteArray(bytes), len, 0, 0); |
1055 | 1066 | writePointerNode.writeToObj(s, CFields.PyCompactUnicodeObject__utf8, mem);
|
1056 | 1067 | writeLongNode.writeToObject(s, CFields.PyCompactUnicodeObject__utf8_length, len);
|
| 1068 | + if (hasSizeProfile.profile(inliningTarget, !lib.isNull(sizePtr))) { |
| 1069 | + writeLongNode.write(sizePtr, len); |
| 1070 | + } |
1057 | 1071 | return mem;
|
| 1072 | + } else { |
| 1073 | + if (hasSizeProfile.profile(inliningTarget, !lib.isNull(sizePtr))) { |
| 1074 | + writeLongNode.write(sizePtr, readI64Node.readFromObj(s, CFields.PyCompactUnicodeObject__utf8_length)); |
| 1075 | + } |
| 1076 | + return utf8; |
1058 | 1077 | }
|
1059 |
| - return utf8; |
1060 | 1078 | }
|
1061 | 1079 |
|
1062 | 1080 | @Fallback
|
1063 |
| - static Object doError(@SuppressWarnings("unused") Object s, |
| 1081 | + @SuppressWarnings("unused") |
| 1082 | + static Object doError(Object s, Object sizePtr, |
1064 | 1083 | @Cached PRaiseNode raiseNode) {
|
1065 | 1084 | throw raiseNode.raise(TypeError, BAD_ARG_TYPE_FOR_BUILTIN_OP);
|
1066 | 1085 | }
|
1067 | 1086 | }
|
1068 | 1087 |
|
1069 |
| - @CApiBuiltin(ret = Py_ssize_t, args = {PyObject}, call = Direct) |
1070 |
| - abstract static class PyTruffle_Unicode_AsUTF8AndSize_Size extends CApiUnaryBuiltinNode { |
1071 |
| - |
1072 |
| - @Specialization |
1073 |
| - Object doUnicode(PString s) { |
1074 |
| - // PyTruffle_Unicode_AsUTF8AndSize_CharPtr must have been be called before |
1075 |
| - return s.getUtf8Bytes().getSequenceStorage().length(); |
1076 |
| - } |
1077 |
| - |
1078 |
| - @Specialization |
1079 |
| - Object doNative(PythonAbstractNativeObject s, |
1080 |
| - @Cached CStructAccess.ReadI64Node readI64Node) { |
1081 |
| - // PyTruffle_Unicode_AsUTF8AndSize_CharPtr must have been be called before |
1082 |
| - return readI64Node.readFromObj(s, CFields.PyCompactUnicodeObject__utf8_length); |
1083 |
| - } |
1084 |
| - } |
1085 |
| - |
1086 | 1088 | @CApiBuiltin(ret = PY_UNICODE_PTR, args = {PyObject}, call = Direct)
|
1087 | 1089 | abstract static class PyTruffle_Unicode_AsUnicodeAndSize_CharPtr extends CApiUnaryBuiltinNode {
|
1088 | 1090 |
|
|
0 commit comments