Le lun. 19 nov. 2018 à 10:48, Antoine Pitrou <solip...@pitrou.net> a écrit : > If the C API only provides Python-level semantics, then it will > roughly have the speed of pure Python (modulo bytecode execution). > > There are important use cases for the C API where it is desired to have > fast type-specific access to Python objects such as tuples, ints, > strings, etc. This is relied upon by modules such as _json and _pickle, > and third-party extensions as well.
Are you sure that using PyDict_GetItem() is really way faster than PyObject_GetItem()? Did someone run a benchmark to have numbers? I would expect that the most common source of speed up of a C extension is the removal of the cost of bytecode evaluation (ceval.c loop). Python internals rely on internals to implement further optimizations, than modifying an "immutable" tuple, bytes or str object, because you can do that at the C level. But I'm not sure that I would like 3rd party extensions to rely on such things. For example, unicodeobject.c uses the following function to check if a str object can be modified in-place, or if a new str object must be created: #ifdef Py_DEBUG static int unicode_is_singleton(PyObject *unicode) { PyASCIIObject *ascii = (PyASCIIObject *)unicode; if (unicode == unicode_empty) return 1; if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) { Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); if (ch < 256 && unicode_latin1[ch] == unicode) return 1; } return 0; } #endif static int unicode_modifiable(PyObject *unicode) { assert(_PyUnicode_CHECK(unicode)); if (Py_REFCNT(unicode) != 1) return 0; if (_PyUnicode_HASH(unicode) != -1) return 0; if (PyUnicode_CHECK_INTERNED(unicode)) return 0; if (!PyUnicode_CheckExact(unicode)) return 0; #ifdef Py_DEBUG /* singleton refcount is greater than 1 */ assert(!unicode_is_singleton(unicode)); #endif return 1; } Victor _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com