Commit 6319cd18 authored by Julian Andres Klode's avatar Julian Andres Klode

apt.Cache: Avoid unneccessary double weakref checks for __getitem__

__getitem__ included an optimization to first check the weakref
dictionary for the key. This optimized performance if __getitem__
was called with a non-canonical name, like foo where foo:amd64 is
the canonical name (the pretty full name).

It, however, vastly decreased performance in the common case. We
do the unification of packages using the canonical name in _rawpkg_to_pkg,
so let's just drop this optimization.
parent 64f06b06
......@@ -260,23 +260,19 @@ class Cache(object):
def __getitem__(self, key):
# type: (object) -> Package
""" look like a dictionary (get key) """
key = str(key)
try:
return self._weakref[key]
key = str(key)
rawpkg = self._cache[key]
except KeyError:
try:
rawpkg = self._cache[key]
except KeyError:
raise KeyError('The cache has no package named %r' % key)
raise KeyError('The cache has no package named %r' % key)
# It might be excluded due to not having a version or something
if not self.__is_real_pkg(rawpkg):
raise KeyError('The cache has no package named %r' % key)
# It might be excluded due to not having a version or something
if not self.__is_real_pkg(rawpkg):
raise KeyError('The cache has no package named %r' % key)
pkg = self._rawpkg_to_pkg(rawpkg)
self._weakref[key] = pkg
pkg = self._rawpkg_to_pkg(rawpkg)
return pkg
return pkg
def get(self, key, default=None):
# type: (object, object) -> Any
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment