From 370e8cfbf55f1abc74943f75e2af9545d7c0d246 Mon Sep 17 00:00:00 2001
From: Grant Mathews <gmathews@atlassian.com>
Date: Thu, 1 Oct 2015 14:45:02 -0700
Subject: [PATCH] Cache results from regex_util.normalize
This should speed up applications that lean heavily on urlresolvers.reverse.
---
django/utils/regex_helper.py | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/django/utils/regex_helper.py b/django/utils/regex_helper.py
index 4a697b2..4424ba0 100644
|
a
|
b
|
should be good enough for a large class of URLS, however.
|
| 7 | 7 | """ |
| 8 | 8 | from __future__ import unicode_literals |
| 9 | 9 | |
| | 10 | from threading import local |
| | 11 | |
| 10 | 12 | from django.utils import six |
| 11 | 13 | from django.utils.six.moves import zip |
| 12 | 14 | |
| … |
… |
class NonCapture(list):
|
| 46 | 48 | Used to represent a non-capturing group in the pattern string. |
| 47 | 49 | """ |
| 48 | 50 | |
| | 51 | _normalized = local() |
| | 52 | _normalized.results = {} |
| 49 | 53 | |
| 50 | 54 | def normalize(pattern): |
| 51 | 55 | """ |
| … |
… |
def normalize(pattern):
|
| 69 | 73 | resolving can be done using positional args when keyword args are |
| 70 | 74 | specified, the two cannot be mixed in the same reverse() call. |
| 71 | 75 | """ |
| | 76 | if pattern not in _normalized.results: |
| | 77 | _normalized.results[pattern] = _normalize(pattern) |
| | 78 | |
| | 79 | # Return a copy of the list to avoid corrupting the cache |
| | 80 | return list(_normalized.results[pattern]) |
| | 81 | |
| | 82 | def _normalize(pattern): |
| 72 | 83 | # Do a linear scan to work out the special features of this pattern. The |
| 73 | 84 | # idea is that we scan once here and collect all the information we need to |
| 74 | 85 | # make future decisions. |