summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan Slenders <jonathan@slenders.be>2016-07-11 22:23:20 +0200
committerJonathan Slenders <jonathan@slenders.be>2016-07-11 22:23:20 +0200
commitc2232e6b39f95df54db99ed884b7d6eee24537b8 (patch)
treea94bfeb7d508349db8297e338fb4d814e6eeb809
parentb911326df83a8d24d59342e91eaa0037a2a71c44 (diff)
Some performance optimizations in filters/base.py
-rw-r--r--prompt_toolkit/filters/base.py53
1 files changed, 20 insertions, 33 deletions
diff --git a/prompt_toolkit/filters/base.py b/prompt_toolkit/filters/base.py
index f82dca6e..6a1a1d0b 100644
--- a/prompt_toolkit/filters/base.py
+++ b/prompt_toolkit/filters/base.py
@@ -29,31 +29,19 @@ class Filter(with_metaclass(ABCMeta, object)):
"""
Chaining of filters using the & operator.
"""
- if isinstance(other, Always) or isinstance(self, Never):
- return self
- elif isinstance(other, Never) or isinstance(self, Always):
- return other
- else:
- assert isinstance(other, Filter), 'Expecting filter, got %r' % other
- return _and(self, other)
+ return _and_cache[self, other]
def __or__(self, other):
"""
Chaining of filters using the | operator.
"""
- if isinstance(other, Always) or isinstance(self, Never):
- return other
- elif isinstance(other, Never) or isinstance(self, Always):
- return self
- else:
- assert isinstance(other, Filter), 'Expecting filter, got %r' % other
- return _or(self, other)
+ return _or_cache[self, other]
def __invert__(self):
"""
Inverting of filters using the ~ operator.
"""
- return _invert(self)
+ return _invert_cache[self]
def __bool__(self):
"""
@@ -86,6 +74,14 @@ class _AndCache(dict):
removed. In practise however, there is a finite amount of filters.
"""
def __missing__(self, filters):
+ a, b = filters
+ assert isinstance(b, Filter), 'Expecting filter, got %r' % b
+
+ if isinstance(b, Always) or isinstance(a, Never):
+ return a
+ elif isinstance(b, Never) or isinstance(a, Always):
+ return b
+
result = _AndList(filters)
self[filters] = result
return result
@@ -94,10 +90,19 @@ class _AndCache(dict):
class _OrCache(dict):
""" Cache for Or operation between filters. """
def __missing__(self, filters):
+ a, b = filters
+ assert isinstance(b, Filter), 'Expecting filter, got %r' % b
+
+ if isinstance(b, Always) or isinstance(a, Never):
+ return b
+ elif isinstance(b, Never) or isinstance(a, Always):
+ return a
+
result = _OrList(filters)
self[filters] = result
return result
+
class _InvertCache(dict):
""" Cache for inversion operator. """
def __missing__(self, filter):
@@ -111,24 +116,6 @@ _or_cache = _OrCache()
_invert_cache = _InvertCache()
-def _and(filter1, filter2):
- """
- And operation between two filters.
- """
- return _and_cache[filter1, filter2]
-
-
-def _or(filter1, filter2):
- """
- Or operation between two filters.
- """
- return _or_cache[filter1, filter2]
-
-
-def _invert(filter):
- return _invert_cache[filter]
-
-
class _AndList(Filter):
"""
Result of &-operation between several filters.