From f0948e7e2e39c659c9363c9fac565c3192fd1aaa Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 21 Apr 2020 22:18:39 +0200 Subject: Micro-optimize global eventFilter Might not make a big difference, but probably worth it, given that this can be called a lot when there are events coming to Qt. Based on a couple of assumptions: - We won't be interested in most events - Very often, events are not going to a QWindow, so we discard them as early as possible. - Very often, it's an event of a type we're not interested in, so we also discard those as early as possible. - "not self._activated" happens rarely. - "if typ not in self._handlers:" is significantly faster than try/except KeyError - try:/except: is only needed around the handler call itself, as we can be reasonably certain the code above won't raise an exception. See #5376 --- qutebrowser/keyinput/eventfilter.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/qutebrowser/keyinput/eventfilter.py b/qutebrowser/keyinput/eventfilter.py index 7916f16fe..992d9f4ce 100644 --- a/qutebrowser/keyinput/eventfilter.py +++ b/qutebrowser/keyinput/eventfilter.py @@ -85,19 +85,22 @@ class EventFilter(QObject): Return: True if the event should be filtered, False if it's passed through. """ + if not isinstance(obj, QWindow): + # We already handled this same event at some point earlier, so + # we're not interested in it anymore. + return False + + typ = event.type() + + if typ not in self._handlers: + return False + + if not self._activated: + return False + + handler = self._handlers[typ] try: - if not self._activated: - return False - if not isinstance(obj, QWindow): - # We already handled this same event at some point earlier, so - # we're not interested in it anymore. - return False - try: - handler = self._handlers[event.type()] - except KeyError: - return False - else: - return handler(event) + return handler(event) except: # If there is an exception in here and we leave the eventfilter # activated, we'll get an infinite loop and a stack overflow. -- cgit v1.2.3-54-g00ecf