[issue46713] Provide a C implementation of collections.abc.KeysView and friends
Raymond Hettinger
report at bugs.python.org
Fri Feb 11 13:55:55 EST 2022
Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:
Some thoughts:
* Other than set operations, most of the pure python code in the dict view ABCs are fast pass throughs. There is no point in rewriting these in C:
def __contains__(self, key):
return key in self._mapping
def __iter__(self):
yield from self._mapping
def __len__(self):
return len(self._mapping)
* For the set operations, the pure python code has only a simple for-loop and contains test. There isn't much fat to be cut:
def __ge__(self, other):
if not isinstance(other, Set):
return NotImplemented
if len(self) < len(other):
return False
for elem in other:
if elem not in self:
return False
return True
* Possibly the eval-loop overhead can be eliminated with Python by using itertools:
def __ge__(self, other):
if not isinstance(other, Set):
return NotImplemented
if len(self) < len(other):
return False
for elem in filterfalse(self.__contains__, other):
return False
return True
* That leaves the question of why the dict views are so much faster (presuming that the posted timings are representative). I haven't looked in detail, but the first candidate that comes to mind is that dictviews_to_set() has a fast path for exact dicts. That lets it bypass the mapping proxy and exploit the fast path in PySet_New for exact dicts. That fast path reuses the stored hash values and exploits knowing that the input has no duplicates. Off-hand, I don't see how that can generalize to the arbitrary mappings supports by the view ABCs.
* Summary: A first look, it doesn't seem like a C rewrite of the view ABCs would bear fruit.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46713>
_______________________________________
More information about the Python-bugs-list
mailing list