Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12389 - performance enhancement of get_models()

Thanks to Travis Cline for the patch, which gives about 35% speed increase
for the testsuite (with SQLite in-memory DB).



git-svn-id: http://code.djangoproject.com/svn/django/trunk@11883 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b38e678dae65823c58cb2be2ac6e4671af524282 1 parent c90b15e
Luke Plant authored December 16, 2009

Showing 1 changed file with 9 additions and 1 deletion. Show diff stats Hide diff stats

  1. 10  django/db/models/loading.py
10  django/db/models/loading.py
@@ -35,6 +35,7 @@ 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):
@@ -140,6 +141,11 @@ def get_models(self, app_mod=None, include_auto_created=False):
140 141
         explicit intermediate table) are not included. However, if you
141 142
         specify include_auto_created=True, they will be.
142 143
         """
  144
+        cache_key = (app_mod, include_auto_created)
  145
+        try:
  146
+            return self._get_models_cache[cache_key]
  147
+        except KeyError:
  148
+            pass
143 149
         self._populate()
144 150
         if app_mod:
145 151
             model_list = self.app_models.get(app_mod.__name__.split('.')[-2], SortedDict()).values()
@@ -148,7 +154,8 @@ def get_models(self, app_mod=None, include_auto_created=False):
148 154
             for app_entry in self.app_models.itervalues():
149 155
                 model_list.extend(app_entry.values())
150 156
         if not include_auto_created:
151  
-            return filter(lambda o: not o._meta.auto_created, model_list)
  157
+            model_list = filter(lambda o: not o._meta.auto_created, model_list)
  158
+        self._get_models_cache[cache_key] = model_list
152 159
         return model_list
153 160
 
154 161
     def get_model(self, app_label, model_name, seed_cache=True):
@@ -183,6 +190,7 @@ def register_models(self, app_label, *models):
183 190
                 if os.path.splitext(fname1)[0] == os.path.splitext(fname2)[0]:
184 191
                     continue
185 192
             model_dict[model_name] = model
  193
+        self._get_models_cache.clear()
186 194
 
187 195
 cache = AppCache()
188 196
 

0 notes on commit b38e678

Please sign in to comment.
Something went wrong with that request. Please try again.