Code

Ticket #12389: get_models_caching.diff

File get_models_caching.diff, 2.0 KB (added by Travis Cline <travis.cline@…>, 5 years ago)
Line 
1From 0225aeaf293236056a074adb12b4a1d19dfa0214 Mon Sep 17 00:00:00 2001
2From: Travis Cline <travis.cline@gmail.com>
3Date: Wed, 16 Dec 2009 10:33:35 -0600
4Subject: [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.
5
6---
7 django/db/models/loading.py |    8 +++++++-
8 1 files changed, 7 insertions(+), 1 deletions(-)
9
10diff --git a/django/db/models/loading.py b/django/db/models/loading.py
11index 4ab1d50..e8ab590 100644
12--- a/django/db/models/loading.py
13+++ b/django/db/models/loading.py
14@@ -35,6 +35,7 @@ class AppCache(object):
15         postponed = [],
16         nesting_level = 0,
17         write_lock = threading.RLock(),
18+        _get_models_cache = {},
19     )
20 
21     def __init__(self):
22@@ -141,6 +142,9 @@ class AppCache(object):
23         specify include_auto_created=True, they will be.
24         """
25         self._populate()
26+        cache_key = '%s-%s' % (app_mod, include_auto_created)
27+        if cache_key in self._get_models_cache:
28+            return self._get_models_cache[cache_key]
29         if app_mod:
30             model_list = self.app_models.get(app_mod.__name__.split('.')[-2], SortedDict()).values()
31         else:
32@@ -148,7 +152,8 @@ class AppCache(object):
33             for app_entry in self.app_models.itervalues():
34                 model_list.extend(app_entry.values())
35         if not include_auto_created:
36-            return filter(lambda o: not o._meta.auto_created, model_list)
37+            model_list = filter(lambda o: not o._meta.auto_created, model_list)
38+        self._get_models_cache[cache_key] = model_list
39         return model_list
40 
41     def get_model(self, app_label, model_name, seed_cache=True):
42@@ -183,6 +188,7 @@ class AppCache(object):
43                 if os.path.splitext(fname1)[0] == os.path.splitext(fname2)[0]:
44                     continue
45             model_dict[model_name] = model
46+        self._get_models_cache.clear()
47 
48 cache = AppCache()
49 
50--
511.6.5.2
52