From a0c7236683e03c9d29be8a6ef2016005d85542e3 Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Mon, 4 Aug 2025 02:02:26 +0000 Subject: [PATCH 1/2] [mypyc] feat: optimize C code for str.count --- mypyc/lib-rt/str_ops.c | 23 ++++------------------- mypyc/primitives/str_ops.py | 4 ++-- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/mypyc/lib-rt/str_ops.c b/mypyc/lib-rt/str_ops.c index a2d10aacea46..bf573347de8b 100644 --- a/mypyc/lib-rt/str_ops.c +++ b/mypyc/lib-rt/str_ops.c @@ -532,28 +532,13 @@ PyObject *CPy_Encode(PyObject *obj, PyObject *encoding, PyObject *errors) { } } -Py_ssize_t CPyStr_Count(PyObject *unicode, PyObject *substring, CPyTagged start) { - Py_ssize_t temp_start = CPyTagged_AsSsize_t(start); - if (temp_start == -1 && PyErr_Occurred()) { - PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG); - return -1; - } +Py_ssize_t CPyStr_Count(PyObject *unicode, PyObject *substring, Py_ssize_t start) { Py_ssize_t end = PyUnicode_GET_LENGTH(unicode); - return PyUnicode_Count(unicode, substring, temp_start, end); + return PyUnicode_Count(unicode, substring, start, end); } -Py_ssize_t CPyStr_CountFull(PyObject *unicode, PyObject *substring, CPyTagged start, CPyTagged end) { - Py_ssize_t temp_start = CPyTagged_AsSsize_t(start); - if (temp_start == -1 && PyErr_Occurred()) { - PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG); - return -1; - } - Py_ssize_t temp_end = CPyTagged_AsSsize_t(end); - if (temp_end == -1 && PyErr_Occurred()) { - PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG); - return -1; - } - return PyUnicode_Count(unicode, substring, temp_start, temp_end); +Py_ssize_t CPyStr_CountFull(PyObject *unicode, PyObject *substring, Py_ssize_t start, Py_ssize_t end) { + return PyUnicode_Count(unicode, substring, start, end); } diff --git a/mypyc/primitives/str_ops.py b/mypyc/primitives/str_ops.py index f07081c6aaa5..1ea4abd03f77 100644 --- a/mypyc/primitives/str_ops.py +++ b/mypyc/primitives/str_ops.py @@ -318,7 +318,7 @@ # str.count(substring, start) method_op( name="count", - arg_types=[str_rprimitive, str_rprimitive, int_rprimitive], + arg_types=[str_rprimitive, str_rprimitive, c_pyssize_t_rprimitive], return_type=c_pyssize_t_rprimitive, c_function_name="CPyStr_Count", error_kind=ERR_NEG_INT, @@ -327,7 +327,7 @@ # str.count(substring, start, end) method_op( name="count", - arg_types=[str_rprimitive, str_rprimitive, int_rprimitive, int_rprimitive], + arg_types=[str_rprimitive, str_rprimitive, c_pyssize_t_rprimitive, c_pyssize_t_rprimitive], return_type=c_pyssize_t_rprimitive, c_function_name="CPyStr_CountFull", error_kind=ERR_NEG_INT, From b719dc0c4c9472fa762111b69609e6c0204bfd84 Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Mon, 4 Aug 2025 02:14:01 +0000 Subject: [PATCH 2/2] fix: headers --- mypyc/lib-rt/CPy.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mypyc/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index 1881aa97f308..c725ea3ff742 100644 --- a/mypyc/lib-rt/CPy.h +++ b/mypyc/lib-rt/CPy.h @@ -753,8 +753,8 @@ bool CPyStr_IsTrue(PyObject *obj); Py_ssize_t CPyStr_Size_size_t(PyObject *str); PyObject *CPy_Decode(PyObject *obj, PyObject *encoding, PyObject *errors); PyObject *CPy_Encode(PyObject *obj, PyObject *encoding, PyObject *errors); -Py_ssize_t CPyStr_Count(PyObject *unicode, PyObject *substring, CPyTagged start); -Py_ssize_t CPyStr_CountFull(PyObject *unicode, PyObject *substring, CPyTagged start, CPyTagged end); +Py_ssize_t CPyStr_Count(PyObject *unicode, PyObject *substring, Py_ssize_t start); +Py_ssize_t CPyStr_CountFull(PyObject *unicode, PyObject *substring, Py_ssize_t start, Py_ssize_t end); CPyTagged CPyStr_Ord(PyObject *obj);