Opened 7 years ago
Last modified 7 years ago
#28828 closed Cleanup/optimization
Performance improvements for build_absolute_uri — at Initial Version
Reported by: | gcbirzan | Owned by: | nobody |
---|---|---|---|
Component: | HTTP handling | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Keryn Knight | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
In cases where a lot of URLs are generated, the performance of it is suboptimal.
- It calls self.get_host() several times. That function does a lot of work that isn't needed more than once.
- It calls urljoin() even on the trivial case of absolute paths (which is the most common case). urljoin is quite expensive as well.
My patch fixes these by using a cached_property for the scheme://host part and by just concatenating the current scheme/host with the location when the location is absolute, doesn't change the host, and doesn't contain '.' or '..'. The last can be implemented in there, but that will just complicate the code I think, for again, a not very common case.
All the tests pass, but they did even when I wasn't checking for '.' and '..', so I added a test for that too.
While the improvements might be minor for some use cases, an example of this being slow can be found in this DRF ticket.