Skip to content

Commit 88c313a

Browse files
committed
Make use of the fact that targets are sorted by slot ID
1 parent 311f3f4 commit 88c313a

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

src/PythonQtSignalReceiver.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
#include <QMetaObject>
4747
#include <QMetaMethod>
4848

49+
#include <algorithm>
50+
4951
// use -2 to signal that the variable is uninitialized
5052
int PythonQtSignalReceiver::_destroyedSignal1Id = -2;
5153
int PythonQtSignalReceiver::_destroyedSignal2Id = -2;
@@ -306,18 +308,18 @@ int PythonQtSignalReceiver::qt_metacall(QMetaObject::Call c, int id, void** argu
306308
// while _targets is modified because a connect/disconnect is done from Python code (which would also hold the GIL)
307309
PYTHONQT_GIL_SCOPE
308310
bool shouldDelete = false;
309-
for (const PythonQtSignalTarget& t : qAsConst(_targets)) {
310-
if (t.slotId() == id) {
311-
const int sigId = t.signalId();
312-
t.call(arguments);
313-
// if the signal is the last destroyed signal, we delete ourselves
314-
if ((sigId == _destroyedSignal1Id) || (sigId == _destroyedSignal2Id)) {
315-
_destroyedSignalCount--;
316-
if (_destroyedSignalCount == 0) {
317-
shouldDelete = true;
318-
}
311+
auto it = std::lower_bound(_targets.begin(), _targets.end(), id,
312+
[](const PythonQtSignalTarget& t, int id) -> bool { return t.slotId() < id; });
313+
if (it != _targets.end() && it->slotId() == id) {
314+
const PythonQtSignalTarget& t = *it;
315+
const int sigId = t.signalId();
316+
t.call(arguments);
317+
// if the signal is the last destroyed signal, we delete ourselves
318+
if ((sigId == _destroyedSignal1Id) || (sigId == _destroyedSignal2Id)) {
319+
_destroyedSignalCount--;
320+
if (_destroyedSignalCount == 0) {
321+
shouldDelete = true;
319322
}
320-
break;
321323
}
322324
}
323325
if (shouldDelete) {

src/PythonQtSignalReceiver.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ class PythonQtSignalReceiver : public PythonQtSignalReceiverBase
138138
int _nextSlotID;
139139
int _nextTargetIndex;
140140
int _destroyedSignalCount;
141-
// Linear list may get slow on multiple targets, but I think typically we have many objects and just a few signals.
142141
// Targets are sorted by slot ID.
143142
QList<PythonQtSignalTarget> _targets;
144143

0 commit comments

Comments
 (0)