Open
Description
- Initially raised as discussion Slow performance when merging cookies #2874
There is a quite significant performance degradation when cookies are being used with requests.
httpx._client.BaseClient._merge_cookies
calls into the cookiejar here:
if cookies or self.cookies:
merged_cookies = Cookies(self.cookies)
merged_cookies.update(cookies)
return merged_cookies
return cookies
When either cookies
or self.cookies
is not None, then httpx._models.Cookies.__bool__
will call into a method called deepvalues
in http.cookiejar.CookieJar
by indirectly calling http.cookiejar.CookieJar.__iter__
.
httpx._models.Cookies.__bool__
:
def __bool__(self) -> bool:
for _ in self.jar:
return True
return False
http.cookiejar.CookieJar.__iter__
:
def __iter__(self):
return deepvalues(self._cookies)
Deepvalues is a significant performance hog because it does a lot of things recursively.
A suggested solution is to check cookies
and self.cookies
for None instead of using __bool__
.
I dropped a performance snapshot to illustrate the problem:
Here deepvalues
takes up almost 17% of the CPU time.