From cfc6d889e77f05b2ff972506c69449807eb98455 Mon Sep 17 00:00:00 2001 From: Mark Maglana Date: Tue, 7 Jul 2015 13:56:09 -0700 Subject: [PATCH] Cache locations to improve list_images performance This change caches the locations in a private attribute to avoid repeated calls for the same result set. The number of calls made is directly proportional to the number of images returned. For a 223 result set, this can reach as high as 99.990 seconds. Caching the locations reduces it to around 2.001 seconds. The risk of stale location data during execution is negligible. --- libcloud/compute/drivers/dimensiondata.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libcloud/compute/drivers/dimensiondata.py b/libcloud/compute/drivers/dimensiondata.py index 0a9525541f..3fca3eeac9 100644 --- a/libcloud/compute/drivers/dimensiondata.py +++ b/libcloud/compute/drivers/dimensiondata.py @@ -536,10 +536,13 @@ def ex_get_location_by_id(self, id): :rtype: :class:`NodeLocation` """ + if not hasattr(self, '_cached_locations_'): + self._cached_locations_ = self.list_locations() + location = None if id is not None: location = list( - filter(lambda x: x.id == id, self.list_locations()))[0] + filter(lambda x: x.id == id, self._cached_locations_))[0] return location def _to_networks(self, object):