Skip to content

Commit 834120a

Browse files
committed
Move PyUnicode_AsUTF8AndSize native fast path to native
1 parent 48fa879 commit 834120a

File tree

3 files changed

+36
-43
lines changed

3 files changed

+36
-43
lines changed

graalpython/com.oracle.graal.python.cext/src/unicodeobject.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3722,6 +3722,7 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
37223722

37233723

37243724
static int unicode_fill_utf8(PyObject *unicode);
3725+
#endif // GraalPy change
37253726

37263727
const char *
37273728
PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
@@ -3730,11 +3731,16 @@ PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
37303731
PyErr_BadArgument();
37313732
return NULL;
37323733
}
3734+
// GraalPy change: upcall for managed objects
3735+
if (points_to_py_handle_space(unicode)) {
3736+
return GraalPyTruffleUnicode_AsUTF8AndSize(unicode, psize);
3737+
}
37333738
if (PyUnicode_READY(unicode) == -1)
37343739
return NULL;
37353740

37363741
if (PyUnicode_UTF8(unicode) == NULL) {
3737-
if (unicode_fill_utf8(unicode) == -1) {
3742+
// GraalPy change: upcall
3743+
if (GraalPyTruffleUnicode_FillUtf8(unicode) == -1) {
37383744
return NULL;
37393745
}
37403746
}
@@ -3743,7 +3749,6 @@ PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
37433749
*psize = PyUnicode_UTF8_LENGTH(unicode);
37443750
return PyUnicode_UTF8(unicode);
37453751
}
3746-
#endif // GraalPy change
37473752

37483753
const char *
37493754
PyUnicode_AsUTF8(PyObject *unicode)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextUnicodeBuiltins.java

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,17 +1022,17 @@ public static _PyUnicode_AsUTF8String create() {
10221022
}
10231023
}
10241024

1025-
@CApiBuiltin(ret = ConstCharPtr, args = {PyObject, PY_SSIZE_T_PTR}, call = Direct)
1026-
abstract static class PyUnicode_AsUTF8AndSize extends CApiBinaryBuiltinNode {
1025+
@CApiBuiltin(ret = ConstCharPtr, args = {PyObject, PY_SSIZE_T_PTR}, call = Ignored)
1026+
abstract static class PyTruffleUnicode_AsUTF8AndSize extends CApiBinaryBuiltinNode {
10271027

10281028
@Specialization
10291029
static Object doUnicode(PString s, Object sizePtr,
10301030
@Bind("this") Node inliningTarget,
1031-
@Shared @CachedLibrary(limit = "2") InteropLibrary lib,
1032-
@Shared @Cached InlinedConditionProfile hasSizeProfile,
1033-
@Shared @Cached InlinedConditionProfile hasUtf8Profile,
1034-
@Shared @Cached CStructAccess.WriteLongNode writeLongNode,
1035-
@Shared @Cached _PyUnicode_AsUTF8String asUTF8String) {
1031+
@CachedLibrary(limit = "2") InteropLibrary lib,
1032+
@Cached InlinedConditionProfile hasSizeProfile,
1033+
@Cached InlinedConditionProfile hasUtf8Profile,
1034+
@Cached CStructAccess.WriteLongNode writeLongNode,
1035+
@Cached _PyUnicode_AsUTF8String asUTF8String) {
10361036
if (hasUtf8Profile.profile(inliningTarget, s.getUtf8Bytes() == null)) {
10371037
PBytes bytes = (PBytes) asUTF8String.execute(s, T_STRICT);
10381038
s.setUtf8Bytes(bytes);
@@ -1043,40 +1043,6 @@ static Object doUnicode(PString s, Object sizePtr,
10431043
return PySequenceArrayWrapper.ensureNativeSequence(s.getUtf8Bytes());
10441044
}
10451045

1046-
@Specialization
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,
1054-
@CachedLibrary(limit = "1") PythonBufferAccessLibrary bufferLib,
1055-
@Cached CStructAccess.ReadPointerNode readPointerNode,
1056-
@Cached CStructAccess.WritePointerNode writePointerNode,
1057-
@Cached CStructAccess.AllocateNode allocateNode,
1058-
@Cached CStructAccess.WriteByteNode writeByteNode,
1059-
@Cached CStructAccess.ReadI64Node readI64Node) {
1060-
Object utf8 = readPointerNode.readFromObj(s, CFields.PyCompactUnicodeObject__utf8);
1061-
if (hasUtf8Profile.profile(inliningTarget, lib.isNull(utf8))) {
1062-
PBytes bytes = (PBytes) asUTF8String.execute(s, T_STRICT);
1063-
int len = bufferLib.getBufferLength(bytes);
1064-
Object mem = allocateNode.alloc(len + 1, true);
1065-
writeByteNode.writeByteArray(mem, bufferLib.getInternalOrCopiedByteArray(bytes), len, 0, 0);
1066-
writePointerNode.writeToObj(s, CFields.PyCompactUnicodeObject__utf8, mem);
1067-
writeLongNode.writeToObject(s, CFields.PyCompactUnicodeObject__utf8_length, len);
1068-
if (hasSizeProfile.profile(inliningTarget, !lib.isNull(sizePtr))) {
1069-
writeLongNode.write(sizePtr, len);
1070-
}
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;
1077-
}
1078-
}
1079-
10801046
@Fallback
10811047
@SuppressWarnings("unused")
10821048
static Object doError(Object s, Object sizePtr,
@@ -1085,6 +1051,27 @@ static Object doError(Object s, Object sizePtr,
10851051
}
10861052
}
10871053

1054+
@CApiBuiltin(ret = Int, args = {PyObject}, call = Ignored)
1055+
abstract static class PyTruffleUnicode_FillUtf8 extends CApiUnaryBuiltinNode {
1056+
1057+
@Specialization
1058+
static Object doNative(PythonAbstractNativeObject s,
1059+
@Cached CStructAccess.WriteLongNode writeLongNode,
1060+
@Cached _PyUnicode_AsUTF8String asUTF8String,
1061+
@CachedLibrary(limit = "1") PythonBufferAccessLibrary bufferLib,
1062+
@Cached CStructAccess.WritePointerNode writePointerNode,
1063+
@Cached CStructAccess.AllocateNode allocateNode,
1064+
@Cached CStructAccess.WriteByteNode writeByteNode) {
1065+
PBytes bytes = (PBytes) asUTF8String.execute(s, T_STRICT);
1066+
int len = bufferLib.getBufferLength(bytes);
1067+
Object mem = allocateNode.alloc(len + 1, true);
1068+
writeByteNode.writeByteArray(mem, bufferLib.getInternalOrCopiedByteArray(bytes), len, 0, 0);
1069+
writePointerNode.writeToObj(s, CFields.PyCompactUnicodeObject__utf8, mem);
1070+
writeLongNode.writeToObject(s, CFields.PyCompactUnicodeObject__utf8_length, len);
1071+
return 0;
1072+
}
1073+
}
1074+
10881075
@CApiBuiltin(ret = PY_UNICODE_PTR, args = {PyObject}, call = Direct)
10891076
abstract static class PyTruffle_Unicode_AsUnicodeAndSize_CharPtr extends CApiUnaryBuiltinNode {
10901077

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CApiFunction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ public final class CApiFunction {
477477
@CApiBuiltin(name = "PyUnicode_AsUCS4", ret = PY_UCS4_PTR, args = {PyObject, PY_UCS4_PTR, Py_ssize_t, Int}, call = CImpl)
478478
@CApiBuiltin(name = "PyUnicode_AsUCS4Copy", ret = PY_UCS4_PTR, args = {PyObject}, call = CImpl)
479479
@CApiBuiltin(name = "PyUnicode_AsUTF8", ret = ConstCharPtrAsTruffleString, args = {PyObject}, call = CImpl)
480+
@CApiBuiltin(name = "PyUnicode_AsUTF8AndSize", ret = ConstCharPtrAsTruffleString, args = {PyObject, PY_SSIZE_T_PTR}, call = CImpl)
480481
@CApiBuiltin(name = "PyUnicode_AsUTF8String", ret = PyObject, args = {PyObject}, call = CImpl)
481482
@CApiBuiltin(name = "PyUnicode_AsUnicode", ret = PY_UNICODE_PTR, args = {PyObject}, call = CImpl)
482483
@CApiBuiltin(name = "PyUnicode_AsUnicodeAndSize", ret = PY_UNICODE_PTR, args = {PyObject, PY_SSIZE_T_PTR}, call = CImpl)

0 commit comments

Comments
 (0)