I'm wondering if restructuring my apps' urlconfs with several nested pattern groups will provide a performance boost for when Django looks for a request-matching pattern. I'm not sure on how to test the performance, but I've tried it out and the new version works at least.
I know that if there is a performance boost, it may as well be negligible. I only started doing this to see if the patterns would be easier to look at as a tree; I'm more worried about screwing things up than boosting speed.
The following is an app's urlconf that is included by the master urlconf at /appname/
.
Old urlconf:
from django.contrib.auth.decorators import login_required
from django.conf.urls.defaults import patterns, include, url
from django.views.generic.simple import redirect_to
from appname import views
urlpatterns = patterns('',
# Actions on sets of reports
url(r'^reports/page(?P<page>[0-9]+)/$', login_required(views.appnameListView.as_view())),
url(r'^reports/bill/', views.bill),
# Actions on individual reports
url(r'^report/new/', views.new),
url(r'^report/(?P<reportNumber>[^/]+)/$', views.detail),
url(r'^report/(?P<reportNumber>[^/]+)/delete/$', views.delete),
url(r'^report/(?P<reportNumber>[^/]+)/edit/$', views.edit),
url(r'^report/(?P<reportNumber>[^/]+)/print/$', views.detail_print),
# url(r'^report/(?P<reportNumber>[^/]+)/pdf/$', views.detail_pdf),
# Configure app settings
url(r'^config/$', views.configure),
url(r'^config/reporttype/(?P<id>[^/]+)/$', views.configure_ReportType),
url(r'^config/reporttype/(?P<id>[^/]+)/delete/$', views.configure_ReportType_delete),
url(r'^config/avgmethod/(?P<name>[^/]+)/$', views.configure_AvgMethod),
url(r'^config/avgmethod/(?P<name>[^/]+)/delete/$', views.configure_AvgMethod_delete),
url(r'^config/datatransformer/(?P<name>[^/]+)/$', views.configure_DataTransformer),
url(r'^config/datatransformer/(?P<name>[^/]+)/delete/$', views.configure_DataTransformer_delete),
# Catch all; #TODO: 404
url(r'^', redirect_to, {'url': '/appname/reports/page1/'}),
)
New urlconf:
from django.contrib.auth.decorators import login_required
from django.conf.urls.defaults import patterns, include, url
from django.views.generic.simple import redirect_to
from appname import views
urlpatterns = patterns('',
# Actions on sets of reports
url(r'^reports/', include(patterns('',
url(r'^page(?P<page>[0-9]+)/$', login_required(views.appnameListView.as_view())),
url(r'^bill/', views.bill),
))),
# Actions on individual reports
url(r'^report/', include(patterns('',
url(r'^new/', views.new),
url(r'^(?P<reportNumber>[^/]+)/$', views.detail),
url(r'^(?P<reportNumber>[^/]+)/delete/$', views.delete),
url(r'^(?P<reportNumber>[^/]+)/edit/$', views.edit),
url(r'^(?P<reportNumber>[^/]+)/print/$', views.detail_print),
# url(r'^(?P<reportNumber>[^/]+)/pdf/$', views.detail_pdf),
))),
# Configure app settings
url(r'^config/', include(patterns('',
url(r'^$', views.configure),
url(r'^reporttype/(?P<id>[^/]+)/$', views.configure_ReportType),
url(r'^reporttype/(?P<id>[^/]+)/delete/$', views.configure_ReportType_delete),
url(r'^avgmethod/(?P<name>[^/]+)/$', views.configure_AvgMethod),
url(r'^avgmethod/(?P<name>[^/]+)/delete/$', views.configure_AvgMethod_delete),
url(r'^datatransformer/(?P<name>[^/]+)/$', views.configure_DataTransformer),
url(r'^datatransformer/(?P<name>[^/]+)/delete/$', views.configure_DataTransformer_delete),
))),
# Catch all; #TODO: 404
url(r'^', redirect_to, {'url': '/appname/reports/page1/'}),
)
I could go even further (4 of the 5 variables checked for with regex are defined at least twice), but I worry about losing the original benefit of readability.
If you do not want to reapeat yourself and follow the DRY-principle, which is sometimes incorporated a lot when developing django code, you should group your urls. So you will reduce the amount duplicate code and produce code that is much easier to maintain. Think for example about the fact, that later on you may want to change the report/ urls to something else, you would have to do it for every single url then!