Ticket #25491: 0001-Cache-results-from-regex_util.normalize.patch

File 0001-Cache-results-from-regex_util.normalize.patch, 1.6 KB (added by atl-gmathews, 9 years ago)

Proposed patch to cache results from normalize()

  • django/utils/regex_helper.py

    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.  
    77"""
    88from __future__ import unicode_literals
    99
     10from threading import local
     11
    1012from django.utils import six
    1113from django.utils.six.moves import zip
    1214
    class NonCapture(list):  
    4648    Used to represent a non-capturing group in the pattern string.
    4749    """
    4850
     51_normalized = local()
     52_normalized.results = {}
    4953
    5054def normalize(pattern):
    5155    """
    def normalize(pattern):  
    6973    resolving can be done using positional args when keyword args are
    7074    specified, the two cannot be mixed in the same reverse() call.
    7175    """
     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
     82def _normalize(pattern):
    7283    # Do a linear scan to work out the special features of this pattern. The
    7384    # idea is that we scan once here and collect all the information we need to
    7485    # make future decisions.
Back to Top