perf: Avoid unnecessary URL processing while parsing links #13132
+28
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There are three optimizations in this commit, in descending order of impact:
If the file URL in the "project detail" response is already absolute, then avoid calling
urljoin()
as it's expensive (mostly because it callsurlparse()
on both of its URL arguments) and does nothing. While it'd be more correct to check whether the file URL has a scheme, we'd need to parse the URL which is what we're trying to avoid in the first place. Anyway, by simply checking if the URL starts withhttp[s]://
, we can avoid slowurljoin()
calls for PyPI responses.Replacing
urllib.parse.urlparse()
withurllib.parse.urlsplit()
in_ensure_quoted_url()
. The URL parsing functions are equivalent for our needs1. However,urlsplit()
isfaster, and we achieve better cache utilization of its internal cache if we call it directly2.Calculating the Link.path property in advance as it's very hot.
Footnotes
we don't care about URL parameters AFAIK (which are different than the query component!) ↩
urlparse()
callsurlsplit()
internally, but it passes theauthority
parameter (unlike any of our calls) so it bypasses the cache. ↩