Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from urlparse() to urlsplit() #793

Merged

Conversation

UnknownPlatypus
Copy link
Contributor

Hi ! This improves the middleware performance by roughly 1-5% (depending on the use case) by replacing urlib.parse.urlparse usage by the almost equivalent urlib.parse.urlsplit which perform 2x faster.

In [5]: %timeit urlparse("https://docs.djangoproject.com/en/4.1/;deprecated=true?q=1&e=2#123")
1.27 µs ± 5.06 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

In [6]: %timeit urlsplit("https://docs.djangoproject.com/en/4.1/;deprecated=true?q=1&e=2#123")
671 ns ± 4.21 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

This is really a slight improvement but since middleware are processed every request, small performance improvements adds up.

@adamchainz
Copy link
Owner

I saw Anthony's video too, I was idly wondering if there was any usage in the open source projects I maintain, thanks for tracking this down.

@adamchainz adamchainz changed the title Use faster urlparse alternative Switch from urlparse() to urlsplit() Sep 12, 2022
@adamchainz adamchainz merged commit 0a17314 into adamchainz:main Sep 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants