Commit 472bcd7e authored by Stefan Behnel's avatar Stefan Behnel

Inline PyObject_GetItem() for a ~10% speedup.

parent 7454b282
...@@ -18,6 +18,8 @@ Features added ...@@ -18,6 +18,8 @@ Features added
* The new TSS C-API in CPython 3.7 is supported and has been backported. * The new TSS C-API in CPython 3.7 is supported and has been backported.
Patch by Naotoshi Seo. (Github issue #1932) Patch by Naotoshi Seo. (Github issue #1932)
* Subscripting (item access) is faster in some cases.
* Some ``bytearray`` operations have been optimised similar to ``bytes``. * Some ``bytearray`` operations have been optimised similar to ``bytes``.
* Safe integer loops (< range(2^30)) are optimised into C loops. * Safe integer loops (< range(2^30)) are optimised into C loops.
......
...@@ -3904,7 +3904,10 @@ class IndexNode(_IndexingBaseNode): ...@@ -3904,7 +3904,10 @@ class IndexNode(_IndexingBaseNode):
function = "__Pyx_PyObject_Dict_GetItem" function = "__Pyx_PyObject_Dict_GetItem"
utility_code = UtilityCode.load_cached("DictGetItem", "ObjectHandling.c") utility_code = UtilityCode.load_cached("DictGetItem", "ObjectHandling.c")
else: else:
function = "PyObject_GetItem" function = "__Pyx_PyObject_GetItem"
code.globalstate.use_utility_code(
TempitaUtilityCode.load_cached("GetItemInt", "ObjectHandling.c"))
utility_code = UtilityCode.load_cached("ObjectGetItem", "ObjectHandling.c")
elif self.type.is_unicode_char and self.base.type is unicode_type: elif self.type.is_unicode_char and self.base.type is unicode_type:
assert self.index.type.is_int assert self.index.type.is_int
function = "__Pyx_GetItemInt_Unicode" function = "__Pyx_GetItemInt_Unicode"
......
...@@ -262,6 +262,49 @@ static CYTHON_INLINE int __Pyx_IterFinish(void) { ...@@ -262,6 +262,49 @@ static CYTHON_INLINE int __Pyx_IterFinish(void) {
#endif #endif
} }
/////////////// ObjectGetItem.proto ///////////////
#if CYTHON_USE_TYPE_SLOTS
static CYTHON_INLINE PyObject *__Pyx_PyObject_GetItem(PyObject *obj, PyObject* key);/*proto*/
#else
#define __Pyx_PyObject_GetItem(obj, key) PyObject_GetItem(obj, key)
#endif
/////////////// ObjectGetItem ///////////////
// //@requires: GetItemInt - added in IndexNode as it uses templating.
#if CYTHON_USE_TYPE_SLOTS
static PyObject *__Pyx_PyObject_GetIndex(PyObject *obj, PyObject* index) {
PySequenceMethods *m = Py_TYPE(obj)->tp_as_sequence;
if (likely(m && m->sq_item)) {
PyObject *runerr;
Py_ssize_t key_value = __Pyx_PyIndex_AsSsize_t(index);
if (likely(key_value != -1 || !(runerr = PyErr_Occurred()))) {
return __Pyx_GetItemInt_Fast(obj, key_value, 0, 1, 1);
}
// Error handling code -- only manage OverflowError differently.
if (PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError)) {
PyErr_Clear();
PyErr_Format(PyExc_IndexError, "cannot fit '%.200s' into an index-sized integer", Py_TYPE(index)->tp_name);
}
} else {
PyErr_Format(PyExc_TypeError, "'%.200s' object is not subscriptable", Py_TYPE(obj)->tp_name);
}
return NULL;
}
static PyObject *__Pyx_PyObject_GetItem(PyObject *obj, PyObject* key) {
PyMappingMethods *m = Py_TYPE(obj)->tp_as_mapping;
if (likely(m && m->mp_subscript)) {
return m->mp_subscript(obj, key);
}
return __Pyx_PyObject_GetIndex(obj, key);
}
#endif
/////////////// DictGetItem.proto /////////////// /////////////// DictGetItem.proto ///////////////
#if PY_MAJOR_VERSION >= 3 && !CYTHON_COMPILING_IN_PYPY #if PY_MAJOR_VERSION >= 3 && !CYTHON_COMPILING_IN_PYPY
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment