diff options
| author | Patryk Zawadzki <patrys@room-303.com> | 2015-11-13 14:37:31 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-11-14 16:43:46 -0500 |
| commit | 7628f87e2b1ab4b8a881f06c8973be4c368aaa3d (patch) | |
| tree | d5ff4a14cc64e74a44caa09fea66480424af01b2 | |
| parent | 6c01010de6e661729e54c050ff9b0449fd493ce7 (diff) | |
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.
| -rw-r--r-- | django/db/models/options.py | 22 |
1 files 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): |
