improve filename_from_url() performance

Manually extracting the part between the last '/' and '?' instead of
relying on the standard libraries' 'urllib.parse.urlsplit()' increases
performance by ~400%.

urlsplit() : 3.64 secs per 1.000.000 iterations
partition(): 0.87 secs per 1.000.000 iterations
This commit is contained in:
Mike Fährmann 2020-10-23 00:04:43 +02:00
parent 968d3e8465
commit a09f42f6b3
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

View File

@ -70,7 +70,7 @@ def ensure_http_scheme(url, scheme="https://"):
def filename_from_url(url):
"""Extract the last part of an URL to use as a filename"""
try:
return urllib.parse.urlsplit(url).path.rpartition("/")[2]
return url.partition("?")[0].rpartition("/")[2]
except (TypeError, AttributeError):
return ""