Opened 9 years ago
Last modified 5 years ago
#27575 new Cleanup/optimization
potential commonmiddleware optimization — at Initial Version
| Reported by: | JorisBenschop | Owned by: | nobody | 
|---|---|---|---|
| Component: | HTTP handling | Version: | 1.10 | 
| Severity: | Normal | Keywords: | |
| Cc: | desecho@… | Triage Stage: | Accepted | 
| Has patch: | no | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | no | 
| Easy pickings: | no | UI/UX: | no | 
Description
please ignore all this if its stupid, but I was trying to create a 1.10 middleware and learn from existing code. Looking at
django.middleware.common.CommonMiddleware I saw this in process_request
host = request.get_host()
must_prepend = settings.PREPEND_WWW and host and not host.startswith('www.')
redirect_url = ('%s://www.%s' % (request.scheme, host)) if must_prepend else ''
If I  understand correctly, this code is ran for each request. Would we not get more performance by setting
redirect_url = ''
if  settings.PREPEND_WWW:
    host = request.get_host()
    if host and not host.startswith('www.')
        redirect_url = ('%s://www.%s' % (request.scheme, host)) 
as this evaluates most of the code only when settings.PREPEND_WWW is true.
Again, maybe I completely misunderstand how python code optimization works. But I hope it helps
  Note:
 See   TracTickets
 for help on using tickets.