From 47a9622599822d5db2dd20f13f369c671c4c4fca Mon Sep 17 00:00:00 2001 From: Christian Tismer Date: Tue, 29 Nov 2022 15:03:16 +0100 Subject: __feature__: Remove the no longer efficient reserved_bits structure The reserved_bits structure is no longer an optimization after moving to PyPy. Accessing any extra field involves always a dict lookup. - remove the reserved_bits field - re-order SbkObjectTypePrivate - replace access functions by currentSelectId() Task-number: PSYIDE-2029 Change-Id: I08642eace9a6399649c039bcc358ce678bbd4fd3 Pick-to: 6.4 Reviewed-by: Friedemann Kleint --- sources/pyside6/libpyside/feature_select.cpp | 15 --------------- sources/pyside6/libpyside/pyside.cpp | 7 ++++--- sources/shiboken6/libshiboken/basewrapper.h | 4 ---- sources/shiboken6/libshiboken/basewrapper_p.h | 17 ++++++++--------- sources/shiboken6/libshiboken/sbkfeature_base.cpp | 21 +++++++-------------- .../shiboken6/libshiboken/signature/signature.cpp | 3 ++- 6 files changed, 21 insertions(+), 46 deletions(-) diff --git a/sources/pyside6/libpyside/feature_select.cpp b/sources/pyside6/libpyside/feature_select.cpp index 17c6169af..695a0e61e 100644 --- a/sources/pyside6/libpyside/feature_select.cpp +++ b/sources/pyside6/libpyside/feature_select.cpp @@ -153,16 +153,6 @@ static inline PyObject *getSelectId(PyObject *dict) return select_id; } -static inline void setCurrentSelectId(PyTypeObject *type, PyObject *select_id) -{ - SbkObjectType_SetReserved(type, PyLong_AsSsize_t(select_id)); -} - -static inline void setCurrentSelectId(PyTypeObject *type, int id) -{ - SbkObjectType_SetReserved(type, id); -} - static bool replaceClassDict(PyTypeObject *type) { /* @@ -184,7 +174,6 @@ static bool replaceClassDict(PyTypeObject *type) // Replace `__dict__` which usually has refcount 1 (but see cyclic_test.py) Py_DECREF(type->tp_dict); type->tp_dict = new_dict; - setCurrentSelectId(type, select_id.object()); return true; } @@ -205,7 +194,6 @@ static bool addNewDict(PyTypeObject *type, PyObject *select_id) setNextDict(dict, new_dict); setNextDict(new_dict, next_dict); type->tp_dict = new_dict; - setCurrentSelectId(type, select_id); return true; } @@ -222,13 +210,11 @@ static inline bool moveToFeatureSet(PyTypeObject *type, PyObject *select_id) // This works because small numbers are singleton objects. if (current_id == select_id) { type->tp_dict = dict; - setCurrentSelectId(type, select_id); return true; } dict = nextInCircle(dict); } while (dict != initial_dict); type->tp_dict = initial_dict; - setCurrentSelectId(type, getSelectId(initial_dict)); return false; } @@ -261,7 +247,6 @@ static bool createNewFeatureSet(PyTypeObject *type, PyObject *select_id) auto id = PyLong_AsSsize_t(select_id); if (id == -1) return false; - setCurrentSelectId(type, id); FeatureProc *proc = featurePointer; for (int idx = id; *proc != nullptr; ++proc, idx >>= 1) { if (idx & 1) { diff --git a/sources/pyside6/libpyside/pyside.cpp b/sources/pyside6/libpyside/pyside.cpp index 672f990bb..a832db28a 100644 --- a/sources/pyside6/libpyside/pyside.cpp +++ b/sources/pyside6/libpyside/pyside.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -232,7 +233,7 @@ static bool _setProperty(PyObject *qObj, PyObject *name, PyObject *value, bool * QByteArray propName(Shiboken::String::toCString(name)); auto type = Py_TYPE(qObj); - int flags = SbkObjectType_GetReserved(type); + int flags = currentSelectId(type); int prop_flag = flags & 0x02; auto found = false; QByteArray getterName{}, setterName{}; @@ -301,7 +302,7 @@ bool fillQtProperties(PyObject *qObj, const QMetaObject *metaObj, PyObject *kwds PyObject *key, *value; Py_ssize_t pos = 0; - int flags = SbkObjectType_GetReserved(Py_TYPE(qObj)); + int flags = currentSelectId(Py_TYPE(qObj)); int snake_flag = flags & 0x01; while (PyDict_Next(kwds, &pos, &key, &value)) { @@ -517,7 +518,7 @@ PyObject *getMetaDataFromQObject(QObject *cppSelf, PyObject *self, PyObject *nam PyErr_Fetch(&type, &value, &traceback); // This was omitted for a loong time. const char *cname = Shiboken::String::toCString(name); - int flags = SbkObjectType_GetReserved(Py_TYPE(self)); + int flags = currentSelectId(Py_TYPE(self)); int snake_flag = flags & 0x01; uint cnameLen = qstrlen(cname); if (std::strncmp("__", cname, 2)) { diff --git a/sources/shiboken6/libshiboken/basewrapper.h b/sources/shiboken6/libshiboken/basewrapper.h index 947bfdd1c..86806c917 100644 --- a/sources/shiboken6/libshiboken/basewrapper.h +++ b/sources/shiboken6/libshiboken/basewrapper.h @@ -61,10 +61,6 @@ typedef void (*SubTypeInitHook)(PyTypeObject *, PyObject *, PyObject *); typedef void (*SelectableFeatureHook)(PyTypeObject *); LIBSHIBOKEN_API SelectableFeatureHook initSelectableFeature(SelectableFeatureHook func); -/// PYSIDE-1019: Get access to PySide reserved bits. -LIBSHIBOKEN_API int SbkObjectType_GetReserved(PyTypeObject *type); -LIBSHIBOKEN_API void SbkObjectType_SetReserved(PyTypeObject *type, int value); - /// PYSIDE-1626: Enforcing a context switch without further action. LIBSHIBOKEN_API void SbkObjectType_UpdateFeature(PyTypeObject *type); diff --git a/sources/shiboken6/libshiboken/basewrapper_p.h b/sources/shiboken6/libshiboken/basewrapper_p.h index fcd22bf2d..fc3cc321c 100644 --- a/sources/shiboken6/libshiboken/basewrapper_p.h +++ b/sources/shiboken6/libshiboken/basewrapper_p.h @@ -97,15 +97,6 @@ struct SbkObjectTypePrivate TypeDiscoveryFuncV2 type_discovery; /// Pointer to a function responsible for deletion of the C++ instance calling the proper destructor. ObjectDestructor cpp_dtor; - /// PYSIDE-1019: Caching the current select Id - int pyside_reserved_bits; // MSVC has bug with the sign bit, so use no bitfield.! - /// True if this type holds two or more C++ instances, e.g.: a Python class which inherits from two C++ classes. - unsigned int is_multicpp : 1; - /// True if this type was defined by the user. - unsigned int is_user_type : 1; - /// Tells is the type is a value type or an object-type, see BEHAVIOUR_ *constants. - unsigned int type_behaviour : 2; - unsigned int delete_in_main_thread : 1; /// C++ name char *original_name; /// Type user data @@ -116,6 +107,14 @@ struct SbkObjectTypePrivate const char **enumFlagInfo; PyObject *enumFlagsDict; PyObject *enumTypeDict; + + /// True if this type holds two or more C++ instances, e.g.: a Python class which inherits from two C++ classes. + unsigned int is_multicpp : 1; + /// True if this type was defined by the user. + unsigned int is_user_type : 1; + /// Tells is the type is a value type or an object-type, see BEHAVIOUR_ *constants. + unsigned int type_behaviour : 2; + unsigned int delete_in_main_thread : 1; }; diff --git a/sources/shiboken6/libshiboken/sbkfeature_base.cpp b/sources/shiboken6/libshiboken/sbkfeature_base.cpp index 3b7439262..9e28d1239 100644 --- a/sources/shiboken6/libshiboken/sbkfeature_base.cpp +++ b/sources/shiboken6/libshiboken/sbkfeature_base.cpp @@ -56,9 +56,13 @@ PyObject *getFeatureSelectId() int currentSelectId(PyTypeObject *type) { - int sel = SbkObjectType_GetReserved(type); - // This could theoretically be -1 if used too early. - assert(sel >= 0); + PyObject *PyId = PyObject_GetAttr(type->tp_dict, PyName::select_id()); + if (PyId == nullptr) { + PyErr_Clear(); + return 0x00; + } + int sel = PyLong_AsLong(PyId); + Py_DECREF(PyId); return sel; } @@ -366,17 +370,6 @@ int SbkObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value) return PyObject_GenericSetAttr(obj, name, value); } -// Caching the select Id. -int SbkObjectType_GetReserved(PyTypeObject *type) -{ - return PepType_SOTP(type)->pyside_reserved_bits; -} - -void SbkObjectType_SetReserved(PyTypeObject *type, int value) -{ - PepType_SOTP(type)->pyside_reserved_bits = value; -} - const char **SbkObjectType_GetPropertyStrings(PyTypeObject *type) { return PepType_SOTP(type)->propertyStrings; diff --git a/sources/shiboken6/libshiboken/signature/signature.cpp b/sources/shiboken6/libshiboken/signature/signature.cpp index 808edc5cf..f83618779 100644 --- a/sources/shiboken6/libshiboken/signature/signature.cpp +++ b/sources/shiboken6/libshiboken/signature/signature.cpp @@ -21,6 +21,7 @@ #include "sbkstring.h" #include "sbkstaticstrings.h" #include "sbkstaticstrings_p.h" +#include "sbkfeature_base.h" #include @@ -534,7 +535,7 @@ static PyObject *adjustFuncName(const char *func_name) // Find the feature flags auto type = reinterpret_cast(obtype.object()); auto dict = type->tp_dict; - int id = SbkObjectType_GetReserved(type); + int id = currentSelectId(type); id = id < 0 ? 0 : id; // if undefined, set to zero auto lower = id & 0x01; auto is_prop = id & 0x02; -- cgit v1.2.3