Skip to content

Commit

Permalink
Improve performance of extend_query
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Oct 17, 2024
1 parent fa83191 commit a86ad84
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES/1305.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved performance of the :py:meth:`~yarl.URL.extend_query` method -- by :user:`bdraco`.
17 changes: 8 additions & 9 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1416,19 +1416,18 @@ def extend_query(self, *args: Any, **kwargs: Any) -> "URL":
>>> url.extend_query(a=3, c=4)
URL('http://example.com/?a=1&b=2&a=3&c=4')
"""
new_query_string = self._get_str_query(*args, **kwargs)
if not new_query_string:
if not (new_query := self._get_str_query(*args, **kwargs)):
return self
if new_query := self._val.query:
scheme, netloc, path, query, fragment = self._val
if query:
# both strings are already encoded so we can use a simple
# string join
if new_query[-1] == "&":
new_query += new_query_string
else:
new_query += f"&{new_query_string}"
query += new_query if query[-1] == "&" else f"&{new_query}"
else:
new_query = new_query_string
return self._from_val(self._val._replace(query=new_query))
query = new_query
return self._from_val(
tuple.__new__(SplitResult, (scheme, netloc, path, query, fragment))
)

@overload
def update_query(self, query: Query) -> "URL": ...
Expand Down

0 comments on commit a86ad84

Please sign in to comment.