Skip to content

Switch from urlparse() to urlsplit()#793

Merged
adamchainz merged 2 commits intoadamchainz:mainfrom
UnknownPlatypus:replace-urlparse-with-urlsplit
Sep 12, 2022
Merged

Switch from urlparse() to urlsplit()#793
adamchainz merged 2 commits intoadamchainz:mainfrom
UnknownPlatypus:replace-urlparse-with-urlsplit

Conversation

@UnknownPlatypus
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread src/corsheaders/middleware.py
@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