diff options
| author | Adrian Herrmann <adrian.herrmann@qt.io> | 2024-01-31 18:03:36 +0100 |
|---|---|---|
| committer | Adrian Herrmann <adrian.herrmann@qt.io> | 2024-02-13 13:28:49 +0100 |
| commit | 194e040570d4d3727b983d93b0e7a4957348a27c (patch) | |
| tree | ec962fdaa7ff4fe0fffff7c506b9cf9778e5602b /sources/pyside6/PySide6 | |
| parent | b806c5e75e89fbe0a3fc53eb82ca1b27524cca69 (diff) | |
QTimer: Call C++ function for singleShot timers
The current implementation of singleshot timers is very old and bypasses
the C++ function. Instead, a timer object is manually created and
started. This incurs a performance penalty, as this bypasses the
optimized code path for 0 timers that eschews a timer object in favor of
directly calling QMetaObject::invokeMethod. This is now fixed, and for 0
timers, the C++ function is called directly.
Change-Id: Idfed06d60eb34355242818ac2df46f75dd27353c
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources/pyside6/PySide6')
| -rw-r--r-- | sources/pyside6/PySide6/QtCore/typesystem_core_common.xml | 6 | ||||
| -rw-r--r-- | sources/pyside6/PySide6/glue/qtcore.cpp | 124 |
2 files changed, 70 insertions, 60 deletions
diff --git a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml index d51a80380..f7235ffc2 100644 --- a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml +++ b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml @@ -2542,10 +2542,10 @@ <include file-name="pysidestaticstrings.h" location="global"/> </extra-includes> <modify-function signature="singleShot(int,const QObject*,const char*)"> - <inject-code class="target" position="beginning" file="../glue/qtcore.cpp" snippet="qtimer-singleshot-1"/> + <inject-code class="target" position="beginning" file="../glue/qtcore.cpp" snippet="qtimer-singleshot-direct-mapping"/> </modify-function> - <add-function signature="singleShot(int,PyCallable*)" static="yes"> - <inject-code class="target" position="beginning" file="../glue/qtcore.cpp" snippet="qtimer-singleshot-2"/> + <add-function signature="singleShot(int@msec@,PyCallable*@functor@)" static="yes"> + <inject-code class="target" position="beginning" file="../glue/qtcore.cpp" snippet="qtimer-singleshot-functor"/> </add-function> <add-function signature="singleShot(int@msec@,const QObject*@context@,PyCallable*@functor@)" static="yes"> <inject-code class="target" position="beginning" file="../glue/qtcore.cpp" snippet="qtimer-singleshot-functor-context"/> diff --git a/sources/pyside6/PySide6/glue/qtcore.cpp b/sources/pyside6/PySide6/glue/qtcore.cpp index 46f794690..7df53d8e8 100644 --- a/sources/pyside6/PySide6/glue/qtcore.cpp +++ b/sources/pyside6/PySide6/glue/qtcore.cpp @@ -988,66 +988,76 @@ auto *ptr = reinterpret_cast<uchar *>(Shiboken::Buffer::getPointer(%PYARG_1, &si %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); // @snippet qtranslator-load -// @snippet qtimer-singleshot-1 -// %FUNCTION_NAME() - disable generation of c++ function call -(void) %2; // remove warning about unused variable +// @snippet qtimer-singleshot-direct-mapping Shiboken::AutoDecRef emptyTuple(PyTuple_New(0)); -auto *timerType = Shiboken::SbkType<QTimer>(); -auto newFunc = reinterpret_cast<newfunc>(PepType_GetSlot(timerType, Py_tp_new)); -auto initFunc = reinterpret_cast<initproc>(PepType_GetSlot(timerType, Py_tp_init)); -auto *pyTimer = newFunc(Shiboken::SbkType<QTimer>(), emptyTuple, nullptr); -initFunc(pyTimer, emptyTuple, nullptr); - -auto timer = %CONVERTTOCPP[QTimer *](pyTimer); -Shiboken::AutoDecRef result( - PyObject_CallMethod(pyTimer, "connect", "OsOs", - pyTimer, - SIGNAL(timeout()), - %PYARG_2, - %3) -); -Shiboken::Object::releaseOwnership(reinterpret_cast<SbkObject *>(pyTimer)); -Py_XDECREF(pyTimer); -timer->setSingleShot(true); -timer->connect(timer, &QTimer::timeout, timer, &QObject::deleteLater); -timer->start(%1); -// @snippet qtimer-singleshot-1 - -// @snippet qtimer-singleshot-2 -// %FUNCTION_NAME() - disable generation of c++ function call -Shiboken::AutoDecRef emptyTuple(PyTuple_New(0)); -auto *timerType = Shiboken::SbkType<QTimer>(); -auto newFunc = reinterpret_cast<newfunc>(PepType_GetSlot(timerType, Py_tp_new)); -auto initFunc = reinterpret_cast<initproc>(PepType_GetSlot(timerType, Py_tp_init)); -auto *pyTimer = newFunc(Shiboken::SbkType<QTimer>(), emptyTuple, nullptr); -initFunc(pyTimer, emptyTuple, nullptr); -QTimer * timer = %CONVERTTOCPP[QTimer *](pyTimer); -timer->setSingleShot(true); - -if (PyObject_TypeCheck(%2, PySideSignalInstance_TypeF())) { - PySideSignalInstance *signalInstance = reinterpret_cast<PySideSignalInstance *>(%2); - Shiboken::AutoDecRef signalSignature(Shiboken::String::fromFormat("2%s", PySide::Signal::getSignature(signalInstance))); - Shiboken::AutoDecRef result( - PyObject_CallMethod(pyTimer, "connect", "OsOO", - pyTimer, - SIGNAL(timeout()), - PySide::Signal::getObject(signalInstance), - signalSignature.object()) - ); +%CPPSELF.%FUNCTION_NAME(%1, %2, %3); +// @snippet qtimer-singleshot-direct-mapping + +// @snippet qtimer-singleshot-functor +auto msec = %1; +if (msec == 0) { + if (PyObject_TypeCheck(%2, PySideSignalInstance_TypeF())) { + auto *signal = %PYARG_2; + auto cppCallback = [signal]() + { + Shiboken::GilState state; + Shiboken::AutoDecRef ret(PyObject_CallMethod(signal, "emit", "()")); + Py_DECREF(signal); + }; + + Py_INCREF(signal); + %CPPSELF.%FUNCTION_NAME(msec, cppCallback); + } else { + Shiboken::AutoDecRef emptyTuple(PyTuple_New(0)); + auto *callable = %PYARG_2; + auto cppCallback = [callable]() + { + Shiboken::GilState state; + Shiboken::AutoDecRef arglist(PyTuple_New(0)); + Shiboken::AutoDecRef ret(PyObject_CallObject(callable, arglist)); + Py_DECREF(callable); + }; + + Py_INCREF(callable); + %CPPSELF.%FUNCTION_NAME(msec, cppCallback); + } } else { - Shiboken::AutoDecRef result( - PyObject_CallMethod(pyTimer, "connect", "OsO", - pyTimer, - SIGNAL(timeout()), - %PYARG_2) - ); -} + // %FUNCTION_NAME() - disable generation of c++ function call + Shiboken::AutoDecRef emptyTuple(PyTuple_New(0)); + auto *timerType = Shiboken::SbkType<QTimer>(); + auto newFunc = reinterpret_cast<newfunc>(PepType_GetSlot(timerType, Py_tp_new)); + auto initFunc = reinterpret_cast<initproc>(PepType_GetSlot(timerType, Py_tp_init)); + auto *pyTimer = newFunc(Shiboken::SbkType<QTimer>(), emptyTuple, nullptr); + initFunc(pyTimer, emptyTuple, nullptr); + + QTimer * timer = %CONVERTTOCPP[QTimer *](pyTimer); + timer->setSingleShot(true); -timer->connect(timer, &QTimer::timeout, timer, &QObject::deleteLater, Qt::DirectConnection); -Shiboken::Object::releaseOwnership(reinterpret_cast<SbkObject *>(pyTimer)); -Py_XDECREF(pyTimer); -timer->start(%1); -// @snippet qtimer-singleshot-2 + if (PyObject_TypeCheck(%2, PySideSignalInstance_TypeF())) { + PySideSignalInstance *signalInstance = reinterpret_cast<PySideSignalInstance *>(%2); + Shiboken::AutoDecRef signalSignature(Shiboken::String::fromFormat("2%s", PySide::Signal::getSignature(signalInstance))); + Shiboken::AutoDecRef result( + PyObject_CallMethod(pyTimer, "connect", "OsOO", + pyTimer, + SIGNAL(timeout()), + PySide::Signal::getObject(signalInstance), + signalSignature.object()) + ); + } else { + Shiboken::AutoDecRef result( + PyObject_CallMethod(pyTimer, "connect", "OsO", + pyTimer, + SIGNAL(timeout()), + %PYARG_2) + ); + } + + timer->connect(timer, &QTimer::timeout, timer, &QObject::deleteLater, Qt::DirectConnection); + Shiboken::Object::releaseOwnership(reinterpret_cast<SbkObject *>(pyTimer)); + Py_XDECREF(pyTimer); + timer->start(msec); +} +// @snippet qtimer-singleshot-functor // @snippet qtimer-singleshot-functor-context auto msec = %1; |
