diff options
author | Christian Tismer <tismer@stackless.com> | 2020-07-08 14:26:04 +0200 |
---|---|---|
committer | Christian Tismer <tismer@stackless.com> | 2020-07-09 11:34:36 +0200 |
commit | fb2dc48389c1099bef3aaed97f16ce2bd1b90fee (patch) | |
tree | fd3266c3f422170df2b92bd494b7e49b7bcf712e /sources | |
parent | 39c6018e484d2a6850d239de03fef89df730f357 (diff) |
shiboken: optimize method override strings
The override strings in bindingmanager were based upon
pure C strings for a long time. There was some mild overhead
of creating PyObject all the time.
With the upcoming name mangling of Selectable Features,
this becomes even a bit more inefficient when strings also get
mangled. With Python strings this is just a dict lookup.
This patch uses the same machinery as `Shiboken::PyName`.
Task-number: PYSIDE-1019
Change-Id: I4eed7222371dadeed0e9ba98cc7970e726ffc044
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'sources')
4 files changed, 12 insertions, 12 deletions
diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp index d0c144358..001c6d902 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp @@ -939,9 +939,10 @@ void CppGenerator::writeVirtualMethodNative(QTextStream &s, s << INDENT << returnStatement(defaultReturnExpr.returnValue()) << Qt::endl; } - s << INDENT << "Shiboken::AutoDecRef " << PYTHON_OVERRIDE_VAR << "(Shiboken::BindingManager::instance().getOverride(this, \""; - s << funcName << "\"));\n"; - + s << INDENT << "static PyObject *pyFuncName = Shiboken::String::createStaticString(\"" + << funcName << "\");\n"; + s << INDENT << "Shiboken::AutoDecRef " << PYTHON_OVERRIDE_VAR + << "(Shiboken::BindingManager::instance().getOverride(this, pyFuncName));\n"; s << INDENT << "if (" << PYTHON_OVERRIDE_VAR << ".isNull()) {\n"; { Indentation indentation(INDENT); diff --git a/sources/shiboken2/libshiboken/bindingmanager.cpp b/sources/shiboken2/libshiboken/bindingmanager.cpp index 1ab8dd089..1c38da81c 100644 --- a/sources/shiboken2/libshiboken/bindingmanager.cpp +++ b/sources/shiboken2/libshiboken/bindingmanager.cpp @@ -273,7 +273,7 @@ SbkObject *BindingManager::retrieveWrapper(const void *cptr) return iter->second; } -PyObject *BindingManager::getOverride(const void *cptr, const char *methodName) +PyObject *BindingManager::getOverride(const void *cptr, PyObject *methodName) { SbkObject *wrapper = retrieveWrapper(cptr); // The refcount can be 0 if the object is dieing and someone called @@ -282,15 +282,14 @@ PyObject *BindingManager::getOverride(const void *cptr, const char *methodName) return nullptr; if (wrapper->ob_dict) { - PyObject *method = PyDict_GetItemString(wrapper->ob_dict, methodName); + PyObject *method = PyDict_GetItem(wrapper->ob_dict, methodName); if (method) { Py_INCREF(reinterpret_cast<PyObject *>(method)); return method; } } - Shiboken::AutoDecRef pyMethodName(Shiboken::String::fromCString(methodName)); - PyObject *method = PyObject_GetAttr(reinterpret_cast<PyObject *>(wrapper), pyMethodName); + PyObject *method = PyObject_GetAttr(reinterpret_cast<PyObject *>(wrapper), methodName); if (method && PyMethod_Check(method) && PyMethod_GET_SELF(method) == reinterpret_cast<PyObject *>(wrapper)) { @@ -302,7 +301,7 @@ PyObject *BindingManager::getOverride(const void *cptr, const char *methodName) for (int i = 1; i < PyTuple_GET_SIZE(mro) - 1; i++) { auto *parent = reinterpret_cast<PyTypeObject *>(PyTuple_GET_ITEM(mro, i)); if (parent->tp_dict) { - defaultMethod = PyDict_GetItem(parent->tp_dict, pyMethodName); + defaultMethod = PyDict_GetItem(parent->tp_dict, methodName); if (defaultMethod && PyMethod_GET_FUNCTION(method) != defaultMethod) return method; } diff --git a/sources/shiboken2/libshiboken/bindingmanager.h b/sources/shiboken2/libshiboken/bindingmanager.h index ba5535347..0bcde196f 100644 --- a/sources/shiboken2/libshiboken/bindingmanager.h +++ b/sources/shiboken2/libshiboken/bindingmanager.h @@ -73,7 +73,7 @@ public: void addToDeletionInMainThread(const DestructorEntry &); SbkObject *retrieveWrapper(const void *cptr); - PyObject *getOverride(const void *cptr, const char *methodName); + PyObject *getOverride(const void *cptr, PyObject *methodName); void addClassInheritance(SbkObjectType *parent, SbkObjectType *child); /** diff --git a/sources/shiboken2/libshiboken/sbkstring.cpp b/sources/shiboken2/libshiboken/sbkstring.cpp index 38bb105d0..7fb7d5730 100644 --- a/sources/shiboken2/libshiboken/sbkstring.cpp +++ b/sources/shiboken2/libshiboken/sbkstring.cpp @@ -41,7 +41,7 @@ #include "sbkstaticstrings_p.h" #include "autodecref.h" -#include <vector> +#include <set> namespace Shiboken { @@ -236,7 +236,7 @@ Py_ssize_t len(PyObject *str) // But this is a global fault in shiboken. We are missing a true // finalization like in all other modules. -using StaticStrings = std::vector<PyObject *>; +using StaticStrings = std::set<PyObject *>; static StaticStrings &staticStrings() { @@ -256,7 +256,7 @@ PyObject *createStaticString(const char *str) PyErr_Print(); Py_FatalError("unexpected error in createStaticString()"); } - staticStrings().push_back(result); + staticStrings().insert(result); return result; } |