diff options
author | Raymond Hettinger <python@rcn.com> | 2002-08-21 01:35:29 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2002-08-21 01:35:29 (GMT) |
commit | de6d6979873b9e7d8066463dff8af4cc65e14e31 (patch) | |
tree | 1e2029f1de5a11b208b02f6ea0739ed9e7b7ccde /Lib/sets.py | |
parent | ede3a0da8bb9eb8230b0e3232a1cdb076b6e32ac (diff) | |
download | cpython-de6d6979873b9e7d8066463dff8af4cc65e14e31.zip cpython-de6d6979873b9e7d8066463dff8af4cc65e14e31.tar.gz cpython-de6d6979873b9e7d8066463dff8af4cc65e14e31.tar.bz2 |
Optimize try/except ordering in sets.py.
Gains a 5:1 speed-up for membership testing by
handling the most common case first (the case
where the element is hashable).
Closes SF Patch 597444.
Diffstat (limited to 'Lib/sets.py')
-rw-r--r-- | Lib/sets.py | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/Lib/sets.py b/Lib/sets.py index 5ed46d1..c678bb4 100644 --- a/Lib/sets.py +++ b/Lib/sets.py @@ -227,12 +227,12 @@ class BaseSet(object): (Called in response to the expression `element in self'.) """ try: - transform = element._as_temporarily_immutable - except AttributeError: - pass - else: - element = transform() - return element in self._data + return element in self._data + except TypeError: + transform = getattr(element, "_as_temporary_immutable", None) + if transform is None: + raise # re-raise the TypeError exception we caught + return transform() in self._data # Subset and superset test @@ -369,14 +369,14 @@ class Set(BaseSet): """Add all values from an iterable (such as a list or file).""" data = self._data value = True - for elt in iterable: + for element in iterable: try: - transform = elt._as_immutable - except AttributeError: - pass - else: - elt = transform() - data[elt] = value + data[element] = value + except TypeError: + transform = getattr(element, "_as_temporary_immutable", None) + if transform is None: + raise # re-raise the TypeError exception we caught + data[transform()] = value def clear(self): """Remove all elements from this set.""" @@ -390,12 +390,12 @@ class Set(BaseSet): This has no effect if the element is already present. """ try: - transform = element._as_immutable - except AttributeError: - pass - else: - element = transform() - self._data[element] = True + self._data[element] = True + except TypeError: + transform = getattr(element, "_as_temporary_immutable", None) + if transform is None: + raise # re-raise the TypeError exception we caught + self._data[transform()] = True def remove(self, element): """Remove an element from a set; it must be a member. @@ -403,12 +403,12 @@ class Set(BaseSet): If the element is not a member, raise a KeyError. """ try: - transform = element._as_temporarily_immutable - except AttributeError: - pass - else: - element = transform() - del self._data[element] + del self._data[element] + except TypeError: + transform = getattr(element, "_as_temporary_immutable", None) + if transform is None: + raise # re-raise the TypeError exception we caught + del self._data[transform()] def discard(self, element): """Remove an element from a set if it is a member. |