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):
|
35 | 35 | postponed = [], |
36 | 36 | nesting_level = 0, |
37 | 37 | write_lock = threading.RLock(), |
| 38 | _get_models_cache = {}, |
38 | 39 | ) |
39 | 40 | |
40 | 41 | def __init__(self): |
… |
… |
class AppCache(object):
|
141 | 142 | specify include_auto_created=True, they will be. |
142 | 143 | """ |
143 | 144 | 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] |
144 | 148 | if app_mod: |
145 | 149 | model_list = self.app_models.get(app_mod.__name__.split('.')[-2], SortedDict()).values() |
146 | 150 | else: |
… |
… |
class AppCache(object):
|
148 | 152 | for app_entry in self.app_models.itervalues(): |
149 | 153 | model_list.extend(app_entry.values()) |
150 | 154 | 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 |
152 | 157 | return model_list |
153 | 158 | |
154 | 159 | def get_model(self, app_label, model_name, seed_cache=True): |
… |
… |
class AppCache(object):
|
183 | 188 | if os.path.splitext(fname1)[0] == os.path.splitext(fname2)[0]: |
184 | 189 | continue |
185 | 190 | model_dict[model_name] = model |
| 191 | self._get_models_cache.clear() |
186 | 192 | |
187 | 193 | cache = AppCache() |
188 | 194 | |