Skip to content

Instantly share code, notes, and snippets.

@tmc
Created April 10, 2009 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmc/93287 to your computer and use it in GitHub Desktop.
Save tmc/93287 to your computer and use it in GitHub Desktop.
From 03573a3876a39de5766082c3557b03e3e7134009 Mon Sep 17 00:00:00 2001
From: Travis Cline <travis.cline@gmail.com>
Date: Fri, 10 Apr 2009 15:13:35 -0500
Subject: [PATCH] added memoization of get_models for performance
---
django/db/models/loading.py | 3 +++
1 files changed, 3 insertions(+), 0 deletions(-)
diff --git a/django/db/models/loading.py b/django/db/models/loading.py
index e07aab4..2743f86 100644
--- a/django/db/models/loading.py
+++ b/django/db/models/loading.py
@@ -4,6 +4,7 @@ from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.datastructures import SortedDict
from django.utils.importlib import import_module
+from django.utils.functional import memoize
import sys
import os
@@ -35,6 +36,7 @@ class AppCache(object):
postponed = [],
nesting_level = 0,
write_lock = threading.RLock(),
+ _get_models_cache = {}
)
def __init__(self):
@@ -144,6 +146,7 @@ class AppCache(object):
for app_entry in self.app_models.itervalues():
model_list.extend(app_entry.values())
return model_list
+ get_models = memoize(get_models, __shared_state['_get_models_cache'], 2)
def get_model(self, app_label, model_name, seed_cache=True):
"""
--
1.6.2.8.g9a668
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment