diff options
author | Florian Bruhin <me@the-compiler.org> | 2020-04-21 22:18:39 +0200 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2020-04-21 22:20:10 +0200 |
commit | f0948e7e2e39c659c9363c9fac565c3192fd1aaa (patch) | |
tree | ea8ff7a186eb7b461f9542ef2f7bbacef7a3a56b | |
parent | cbc835acb721840236f1abd5006e11ebb1810495 (diff) | |
download | qutebrowser-f0948e7e2e39c659c9363c9fac565c3192fd1aaa.tar.gz qutebrowser-f0948e7e2e39c659c9363c9fac565c3192fd1aaa.zip |
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
-rw-r--r-- | qutebrowser/keyinput/eventfilter.py | 27 |
1 files 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. |