From 0f35e4b7b9cbfc78642c48c9477b0e9b7e99fb6d Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Wed, 14 Aug 2019 03:43:52 -0400 Subject: [PATCH] apt_facts - Fix cache related performance regression (#60511) * apt_facts - Fix cache related performance regression * Another minor performance improvement --- .../fragments/package_facts-performace-regression-fix.yaml | 2 ++ lib/ansible/modules/packaging/os/package_facts.py | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/package_facts-performace-regression-fix.yaml diff --git a/changelogs/fragments/package_facts-performace-regression-fix.yaml b/changelogs/fragments/package_facts-performace-regression-fix.yaml new file mode 100644 index 00000000000..7ad167f00d9 --- /dev/null +++ b/changelogs/fragments/package_facts-performace-regression-fix.yaml @@ -0,0 +1,2 @@ +bugfixes: + - apt_facts - fix performance regression when getting facts about apt packages (https://github.com/ansible/ansible/issues/60450) diff --git a/lib/ansible/modules/packaging/os/package_facts.py b/lib/ansible/modules/packaging/os/package_facts.py index 1c7fbb3192a..a0032972bc9 100644 --- a/lib/ansible/modules/packaging/os/package_facts.py +++ b/lib/ansible/modules/packaging/os/package_facts.py @@ -192,7 +192,7 @@ class APT(LibMgr): @property def pkg_cache(self): - if self._cache: + if self._cache is not None: return self._cache self._cache = self._lib.Cache() @@ -209,7 +209,9 @@ class APT(LibMgr): return we_have_lib def list_installed(self): - return [pk for pk in self.pkg_cache.keys() if self.pkg_cache[pk].is_installed] + # Store the cache to avoid running pkg_cache() for each item in the comprehension, which is very slow + cache = self.pkg_cache + return [pk for pk in cache.keys() if cache[pk].is_installed] def get_package_details(self, package): ac_pkg = self.pkg_cache[package].installed