On Thu, 2007-06-14 at 14:17 -0700, Smoo.Master wrote:
> I have using django to make a sort of library website.
> 
> Since I am mostly using generic views, I use the {% url %} tag (which
> calls reverse) with named url patterns in my templates. However this
> became quite slow (as I verified with the hotshot profiler). On a page
> with several hundred links, it takes thirty seconds just to resolve
> the links. Even on a page with just a few links, it takes a large
> fraction of a second. This is true on both the development server and
> apache with mod_python.
> 
> Hardcoding the url patterns in the templates fixes the performance
> problems, but makes them brittle to changes in the url patterns.
> 
> any suggestions?

Make reverse() faster. :-)

The execution time is related to the number of URL patterns. In fact,
it's O(n^2) for uniformly random lookups (O(n) best case and O(n^2)
worst case if you work through the details).

There are at least a couple of optimisation possibilities in reverse()
that would cut down on the work; mostly just a matter of finding time,
or a volunteer, to work on them.

A couple I know of:
        - have a module-level cache of RegexURLResolver classes keyed
        off the urlconf file so that we don't have to compile it every
        time. That would save an O(n) factor (for both forwards and
        backwards lookups).
        
        - convert the lookup in RegexURLResolver to be hashed-based off
        the url name (and view function), rather than a linear scan of
        the patterns dictionary. The current behaviour is poor for large
        lists of url patterns where you aren't matching against the
        early entries. This would change the O(n) scan time into O(1)
        for reverse lookups.
        
I suspect forward lookups can't be better than O(n) unless one was to
get unbelievably tricky doing a regex rewrite and that's a much harder
problem and even then it will be O(n) worst case, still.

However, both of the above changes wouldn't be too hard and don't have a
downside.

If you (or somebody else) wants to work on this, it should only be an
evening or so of work to make the changes and test that it still works
(and is faster).

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to