onespacemedia / cms

A collection of Django extensions that add content-management facilities to Django projects.
BSD 3-Clause "New" or "Revised" License
14 stars 7 forks source link

Speed up RequestPageManager. #160

Closed lewiscollard closed 4 years ago

lewiscollard commented 5 years ago

Previously, alternate_page_version on RequestPageManager (which is quietly called by the current property, among others) was attempting to find alternative versions of the current page for the requests current country, regardless of whether the current request was localised (i.e. whether the localisation system is activated or not). In this pull, the country is checked for emptiness and does not look for alternative versions if it's unset.

This pull also turns @property into @cached_property (consistent ~10ms speedup). Someone should check my working, but this should always be safe as:

1) The lifetime of RequestPageManager is one request/response 2) The page structure rarely differs between the start and end of a request 3) I think the cases in which this happens, which is to say editing pages as an admin, you're probably not accessing request.pages.homepage, request.pages.section etc immediately after saving an object.

Testing this on a couple of IRL projects shows a roughly 80ms improvement in TTFB locally, and 50ms with Cachalot and Memcached (most of the improvement comes from the alternate_page_version change).