Commit 78421036 authored by Julian Andres Klode's avatar Julian Andres Klode

apt.Cache: Use weakref.setdefault to find existing Package objects

This improves performance in the iteration, our general assumption
here is now that no weak reference to the package exists, and hence
we avoid an extra dictionary lookup at the expense of creating another
package object.
parent 6319cd18
Pipeline #12740 passed with stages
in 13 minutes and 16 seconds
......@@ -292,12 +292,8 @@ class Cache(object):
.. versionadded:: 1.0.0
"""
fullname = rawpkg.get_fullname(pretty=False)
try:
pkg = self._weakref[fullname]
except KeyError:
pkg = Package(self, rawpkg)
self._weakref[fullname] = pkg
return pkg
return self._weakref.setdefault(fullname, Package(self, rawpkg))
def __iter__(self):
# type: () -> Iterator[Package]
......@@ -307,7 +303,8 @@ class Cache(object):
# is disastrous if we use compressed package indexes, and slower than
# necessary for uncompressed indexes.
for pkgname in self.keys():
yield self[pkgname]
pkg = Package(self, self._cache[pkgname])
yield self._weakref.setdefault(pkgname, pkg)
def __is_real_pkg(self, rawpkg):
# type: (apt_pkg.Package) -> bool
......
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