#27575: potential commonmiddleware optimization
-------------------------------------+-------------------------------------
     Reporter:  JorisBenschop        |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Core (Other)         |                  Version:  1.10
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  1                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by JorisBenschop:

Old 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

New 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

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27575#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" 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].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/071.8ddd9b415e6c794b13a28b619c0ed9ef%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to