summaryrefslogtreecommitdiff
path: root/django/core
diff options
context:
space:
mode:
authorBouke Haarsma <bouke@webatoom.nl>2013-11-01 21:15:41 +0100
committerBaptiste Mispelon <bmispelon@gmail.com>2013-11-11 08:53:09 +0100
commit9b7455e918a437c3db91e88dcbf6d9c93fef96f8 (patch)
tree4cda7466ec20e5255633e7447eaa03ad5450bf24 /django/core
parent6c5f5b9a414b8bdfafc45db5710acf200cca9885 (diff)
Fixed #21351 -- Replaced memoize with Python's lru_cache.
Replaced the custom, untested memoize with a similar decorator from Python's 3.2 stdlib. Although some minor performance degradation (see ticket), it is expected that in the long run lru_cache will outperform memoize once it is implemented in C. Thanks to EvilDMP for the report and Baptiste Mispelon for the idea of replacing memoize with lru_cache.
Diffstat (limited to 'django/core')
-rw-r--r--django/core/management/commands/loaddata.py9
-rw-r--r--django/core/urlresolvers.py23
2 files changed, 12 insertions, 20 deletions
diff --git a/django/core/management/commands/loaddata.py b/django/core/management/commands/loaddata.py
index 72c5d0854f..6aec0c5703 100644
--- a/django/core/management/commands/loaddata.py
+++ b/django/core/management/commands/loaddata.py
@@ -14,8 +14,9 @@ from django.core.management.color import no_style
from django.db import (connections, router, transaction, DEFAULT_DB_ALIAS,
IntegrityError, DatabaseError)
from django.db.models import get_app_paths
+from django.utils import lru_cache
from django.utils.encoding import force_text
-from django.utils.functional import cached_property, memoize
+from django.utils.functional import cached_property
from django.utils._os import upath
from itertools import product
@@ -164,7 +165,8 @@ class Command(BaseCommand):
RuntimeWarning
)
- def _find_fixtures(self, fixture_label):
+ @lru_cache.lru_cache(maxsize=None)
+ def find_fixtures(self, fixture_label):
"""
Finds fixture files for a given label.
"""
@@ -220,9 +222,6 @@ class Command(BaseCommand):
return fixture_files
- _label_to_fixtures_cache = {}
- find_fixtures = memoize(_find_fixtures, _label_to_fixtures_cache, 2)
-
@cached_property
def fixture_dirs(self):
"""
diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py
index 13919144e5..634eb7519a 100644
--- a/django/core/urlresolvers.py
+++ b/django/core/urlresolvers.py
@@ -16,18 +16,14 @@ from django.http import Http404
from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
from django.utils.datastructures import MultiValueDict
from django.utils.encoding import force_str, force_text, iri_to_uri
-from django.utils.functional import memoize, lazy
+from django.utils.functional import lazy
from django.utils.http import urlquote
from django.utils.module_loading import module_has_submodule
from django.utils.regex_helper import normalize
-from django.utils import six
+from django.utils import six, lru_cache
from django.utils.translation import get_language
-_resolver_cache = {} # Maps URLconf modules to RegexURLResolver instances.
-_ns_resolver_cache = {} # Maps namespaces to RegexURLResolver instances.
-_callable_cache = {} # Maps view and url pattern names to their view functions.
-
# SCRIPT_NAME prefixes for each thread are stored here. If there's no entry for
# the current thread (which is the only one we ever access), it is assumed to
# be empty.
@@ -80,6 +76,7 @@ class NoReverseMatch(Exception):
pass
+@lru_cache.lru_cache(maxsize=None)
def get_callable(lookup_view, can_fail=False):
"""
Convert a string version of a function name to the callable object.
@@ -119,17 +116,17 @@ def get_callable(lookup_view, can_fail=False):
"Could not import %s. View does not exist in module %s." %
(lookup_view, mod_name))
return lookup_view
-get_callable = memoize(get_callable, _callable_cache, 1)
+@lru_cache.lru_cache(maxsize=None)
def get_resolver(urlconf):
if urlconf is None:
from django.conf import settings
urlconf = settings.ROOT_URLCONF
return RegexURLResolver(r'^/', urlconf)
-get_resolver = memoize(get_resolver, _resolver_cache, 1)
+@lru_cache.lru_cache(maxsize=None)
def get_ns_resolver(ns_pattern, resolver):
# Build a namespaced resolver for the given parent urlconf pattern.
# This makes it possible to have captured parameters in the parent
@@ -137,7 +134,6 @@ def get_ns_resolver(ns_pattern, resolver):
ns_resolver = RegexURLResolver(ns_pattern,
resolver.url_patterns)
return RegexURLResolver(r'^/', [ns_resolver])
-get_ns_resolver = memoize(get_ns_resolver, _ns_resolver_cache, 2)
def get_mod_func(callback):
@@ -523,12 +519,9 @@ reverse_lazy = lazy(reverse, str)
def clear_url_caches():
- global _resolver_cache
- global _ns_resolver_cache
- global _callable_cache
- _resolver_cache.clear()
- _ns_resolver_cache.clear()
- _callable_cache.clear()
+ get_callable.cache_clear()
+ get_resolver.cache_clear()
+ get_ns_resolver.cache_clear()
def set_script_prefix(prefix):