From 7628f87e2b1ab4b8a881f06c8973be4c368aaa3d Mon Sep 17 00:00:00 2001 From: Patryk Zawadzki Date: Fri, 13 Nov 2015 14:37:31 +0100 Subject: [PATCH] Fixed #25750 -- Made Options._expire_cache() faster Avoided unnecessary list operations and delattr() calls that result in an exception being raised as it causes call frame reconstruction which is very costly, especially so in a function that is called millions of times. --- django/db/models/options.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/django/db/models/options.py b/django/db/models/options.py index 9c69387c96..5dd3a815de 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -65,9 +65,9 @@ def make_immutable_fields_list(name, data): @python_2_unicode_compatible class Options(object): - FORWARD_PROPERTIES = ('fields', 'many_to_many', 'concrete_fields', - 'local_concrete_fields', '_forward_fields_map') - REVERSE_PROPERTIES = ('related_objects', 'fields_map', '_relation_tree') + FORWARD_PROPERTIES = {'fields', 'many_to_many', 'concrete_fields', + 'local_concrete_fields', '_forward_fields_map'} + REVERSE_PROPERTIES = {'related_objects', 'fields_map', '_relation_tree'} def __init__(self, meta, app_label=None): self._get_fields_cache = {} @@ -586,18 +586,14 @@ class Options(object): def _expire_cache(self, forward=True, reverse=True): # This method is usually called by apps.cache_clear(), when the # registry is finalized, or when a new field is added. - properties_to_expire = [] if forward: - properties_to_expire.extend(self.FORWARD_PROPERTIES) + for cache_key in self.FORWARD_PROPERTIES: + if cache_key in self.__dict__: + delattr(self, cache_key) if reverse and not self.abstract: - properties_to_expire.extend(self.REVERSE_PROPERTIES) - - for cache_key in properties_to_expire: - try: - delattr(self, cache_key) - except AttributeError: - pass - + for cache_key in self.REVERSE_PROPERTIES: + if cache_key in self.__dict__: + delattr(self, cache_key) self._get_fields_cache = {} def get_fields(self, include_parents=True, include_hidden=False):