On Mon, Sep 23, 2013 at 5:55 AM, Anssi Kääriäinen <[email protected]> wrote: > On 09/20/2013 06:29 PM, Tom Evans wrote: >> >> On Fri, Sep 20, 2013 at 4:13 PM, Florian Apolloner >> <[email protected]> wrote: >>>> >>>> It seems more sensible to hook something that has the lifetime of the >>>> request to the request, rather than stick it in TLS, keyed to the >>>> thread serving the request. >>> >>> >>> Jupp, sadly I don't see a sensible way around thread local storage here >>> :( >>> >> All good points, I just have this mental "HMM" when I see TLS as the >> solution for anything. TLS is already used for things like the >> language code of the current request, which avoids you having to pass >> around a state object or passing down lang_code all down the stack, >> but means that things like URL reversing and resolving benchmarks are >> slow (O(n²)) with USE_I18N on. > > > Huh? What is the n here? And why would passing the lang_code down the stack > help? > > - Anssi
n is the number of URLs. In LocaleRegexProvider get_language() is called each time the reqexp is requested - not just each time the regexp is compiled. LocaleRegexProvider being the base class of RegexURLPattern and RegexURLResolver https://github.com/django/django/blob/master/django/core/urlresolvers.py#L160 Each time get_language() is called you do a TLS get. If you have a large number of URLs, get_language() is called once for each URL pattern that is checked against the current URL. If you have thousands of URLs, and the current URL does not match (or matches one of the last ones listed), then you have thousands of unnecessary TLS fetches per URL resolve. The fetches are unnecessary since the layer above could look up the current language once, and pass that down. That pattern would not allow the magic for regex() to be hidden behind a @property, and so instead expensive TLS lookups occur inside regex() instead of inside the caller of regex(). See also "URL dispatcher slow?" from last October, and: http://mindref.blogspot.co.uk/2012/10/python-web-routing-benchmark.html Cheers Tom -- You received this message because you are subscribed to the Google Groups "Django developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/django-developers. For more options, visit https://groups.google.com/groups/opt_out.
