0
0
mirror of https://github.com/django/django.git synced 2024-11-30 15:10:46 +01:00

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.
This commit is contained in:
Patryk Zawadzki 2015-11-13 14:37:31 +01:00 committed by Tim Graham
parent 6c01010de6
commit 7628f87e2b

View File

@ -65,9 +65,9 @@ def make_immutable_fields_list(name, data):
@python_2_unicode_compatible @python_2_unicode_compatible
class Options(object): class Options(object):
FORWARD_PROPERTIES = ('fields', 'many_to_many', 'concrete_fields', FORWARD_PROPERTIES = {'fields', 'many_to_many', 'concrete_fields',
'local_concrete_fields', '_forward_fields_map') 'local_concrete_fields', '_forward_fields_map'}
REVERSE_PROPERTIES = ('related_objects', 'fields_map', '_relation_tree') REVERSE_PROPERTIES = {'related_objects', 'fields_map', '_relation_tree'}
def __init__(self, meta, app_label=None): def __init__(self, meta, app_label=None):
self._get_fields_cache = {} self._get_fields_cache = {}
@ -586,18 +586,14 @@ class Options(object):
def _expire_cache(self, forward=True, reverse=True): def _expire_cache(self, forward=True, reverse=True):
# This method is usually called by apps.cache_clear(), when the # This method is usually called by apps.cache_clear(), when the
# registry is finalized, or when a new field is added. # registry is finalized, or when a new field is added.
properties_to_expire = []
if forward: 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: if reverse and not self.abstract:
properties_to_expire.extend(self.REVERSE_PROPERTIES) for cache_key in self.REVERSE_PROPERTIES:
if cache_key in self.__dict__:
for cache_key in properties_to_expire: delattr(self, cache_key)
try:
delattr(self, cache_key)
except AttributeError:
pass
self._get_fields_cache = {} self._get_fields_cache = {}
def get_fields(self, include_parents=True, include_hidden=False): def get_fields(self, include_parents=True, include_hidden=False):