Originally opened by @gyula-lakatos on 2023-10-02 17:16:44 in encode/httpx
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 to None instead of using __bool__.
I dropped a performance snapshot to illustrate the problem:

Here deepvalues takes up almost 17% of the CPU time.
There is a quite significant performance degradation when cookies are being used with requests.
httpx._client.BaseClient._merge_cookiescalls into the cookiejar here:When either
cookiesorself.cookiesis not None, thenhttpx._models.Cookies.__bool__will call into a method calleddeepvaluesin http.cookiejar.CookieJarby indirectly calling http.cookiejar.CookieJar.__iter__.httpx._models.Cookies.__bool__:http.cookiejar.CookieJar.__iter__:Deepvalues is a significant performance hog because it does a lot of things recursively.
A suggested solution is to check
cookiesandself.cookiesto None instead of using__bool__.I dropped a performance snapshot to illustrate the problem:

Here
deepvaluestakes up almost 17% of the CPU time.