Opened 6 months ago

Last modified 6 months ago

#35467 closed Cleanup/optimization

Prefer urlsplit() over urlparse() — at Version 1

Reported by: Adam Johnson Owned by: nobody
Component: Utilities Version: dev
Severity: Normal Keywords:
Cc: 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 (last modified by Adam Johnson)

Many places in Django use [urlparse()](https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlparse), which supports the rarely-used “path parameter” syntax (not to be confused with query parameters). The urlsplit() function is similar but does not parse such path parameters, which makes it a bit faster.

I think most or all calls to urlparse() can be replaced with urlsplit(), and similarly urlunparse() with urlunsplit(). This may make a small but measurable performance difference in common paths, such as in CsrfViewMiddleware or the test Client.

See more in this Anthony Sottile video: https://www.youtube.com/watch?v=ABJvdsIANds , where he reports a 3% import time improvement on the Stripe project.

Change History (1)

comment:1 by Adam Johnson, 6 months ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top