Ticket #12389: get_models_caching.diff

File get_models_caching.diff, 2.0 KB (added by Travis Cline <travis.cline@…>, 15 years ago)
  • django/db/models/loading.py

    From 0225aeaf293236056a074adb12b4a1d19dfa0214 Mon Sep 17 00:00:00 2001
    From: Travis Cline <travis.cline@gmail.com>
    Date: Wed, 16 Dec 2009 10:33:35 -0600
    Subject: [PATCH] Added get_models caching for test performance. In my tests I see about a 30% speedup in Django's testsuite and closer to 40% in a large commercial testsuite.
    
    ---
     django/db/models/loading.py |    8 +++++++-
     1 files changed, 7 insertions(+), 1 deletions(-)
    
    diff --git a/django/db/models/loading.py b/django/db/models/loading.py
    index 4ab1d50..e8ab590 100644
    a b class AppCache(object):  
    3535        postponed = [],
    3636        nesting_level = 0,
    3737        write_lock = threading.RLock(),
     38        _get_models_cache = {},
    3839    )
    3940
    4041    def __init__(self):
    class AppCache(object):  
    141142        specify include_auto_created=True, they will be.
    142143        """
    143144        self._populate()
     145        cache_key = '%s-%s' % (app_mod, include_auto_created)
     146        if cache_key in self._get_models_cache:
     147            return self._get_models_cache[cache_key]
    144148        if app_mod:
    145149            model_list = self.app_models.get(app_mod.__name__.split('.')[-2], SortedDict()).values()
    146150        else:
    class AppCache(object):  
    148152            for app_entry in self.app_models.itervalues():
    149153                model_list.extend(app_entry.values())
    150154        if not include_auto_created:
    151             return filter(lambda o: not o._meta.auto_created, model_list)
     155            model_list = filter(lambda o: not o._meta.auto_created, model_list)
     156        self._get_models_cache[cache_key] = model_list
    152157        return model_list
    153158
    154159    def get_model(self, app_label, model_name, seed_cache=True):
    class AppCache(object):  
    183188                if os.path.splitext(fname1)[0] == os.path.splitext(fname2)[0]:
    184189                    continue
    185190            model_dict[model_name] = model
     191        self._get_models_cache.clear()
    186192
    187193cache = AppCache()
    188194
Back to Top