Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR #7819/dfc3f899 backport][3.9] Skip filtering CookieJar when the jar is empty or all cookies have expired #7822

Conversation

Rongronggg9
Copy link
Contributor

This is a backport of PR #7819 as merged into master (dfc3f89).

What do these changes do?

The filtering itself and its preparation in CookieJar.filter_cookies() is expensive. Sometimes there are no cookies in the jar or all cookies have expired. Skip filtering and its preparation in this case.

Because the empty check is much cheaper than _do_expiration(), I think it deserves to be duplicated before and after calling _do_expiration().

$ python3.11 -m timeit -s 'from collections import defaultdict; d=defaultdict(foo="bar")' \
> 'if not d: pass'
50000000 loops, best of 5: 8.3 nsec per loop
$ python3.11 -m timeit -s 'from collections import defaultdict; d=defaultdict()' \
> 'if not d: pass'
50000000 loops, best of 5: 8.74 nsec per loop
$ python3.11 -m timeit -s 'from aiohttp import CookieJar; cj = CookieJar()' \
> 'cj._do_expiration()'
200000 loops, best of 5: 1.86 usec per loop

Are there changes in behavior for the user?

No.

Related issue number

#7583 (comment)

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> for example (588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."

…e expired (aio-libs#7819)

The filtering itself and its preparation in `CookieJar.filter_cookies()`
is expensive. Sometimes there are no cookies in the jar or all cookies
have expired. Skip filtering and its preparation in this case.

Because the empty check is much cheaper than `_do_expiration()`, I think
it deserves to be duplicated before and after calling
`_do_expiration()`.

```console
$ python3.11 -m timeit -s 'from collections import defaultdict; d=defaultdict(foo="bar")' \
> 'if not d: pass'
50000000 loops, best of 5: 8.3 nsec per loop
$ python3.11 -m timeit -s 'from collections import defaultdict; d=defaultdict()' \
> 'if not d: pass'
50000000 loops, best of 5: 8.74 nsec per loop
$ python3.11 -m timeit -s 'from aiohttp import CookieJar; cj = CookieJar()' \
> 'cj._do_expiration()'
200000 loops, best of 5: 1.86 usec per loop
```

(cherry picked from commit dfc3f89)
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Nov 12, 2023
Copy link

codecov bot commented Nov 12, 2023

Codecov Report

Merging #7822 (e6c071a) into 3.9 (fffc433) will decrease coverage by 0.01%.
The diff coverage is 66.66%.

@@            Coverage Diff             @@
##              3.9    #7822      +/-   ##
==========================================
- Coverage   97.36%   97.36%   -0.01%     
==========================================
  Files         107      107              
  Lines       32325    32329       +4     
  Branches     3826     3828       +2     
==========================================
+ Hits        31474    31476       +2     
- Misses        648      649       +1     
- Partials      203      204       +1     
Flag Coverage Δ
CI-GHA 97.27% <66.66%> (-0.01%) ⬇️
OS-Linux 96.95% <66.66%> (-0.02%) ⬇️
OS-Windows 94.50% <66.66%> (-0.01%) ⬇️
OS-macOS 96.62% <66.66%> (-0.01%) ⬇️
Py-3.10.11 94.41% <66.66%> (-0.01%) ⬇️
Py-3.10.13 96.79% <66.66%> (-0.01%) ⬇️
Py-3.11.6 96.47% <66.66%> (-0.01%) ⬇️
Py-3.12.0 ?
Py-3.8.10 94.38% <66.66%> (-0.01%) ⬇️
Py-3.8.18 96.72% <66.66%> (-0.01%) ⬇️
Py-3.9.13 94.39% <66.66%> (-0.01%) ⬇️
Py-3.9.18 96.77% <66.66%> (-0.01%) ⬇️
Py-pypy7.3.11 96.16% <66.66%> (-0.01%) ⬇️
VM-macos 96.62% <66.66%> (-0.01%) ⬇️
VM-ubuntu 96.95% <66.66%> (-0.02%) ⬇️
VM-windows 94.50% <66.66%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
aiohttp/cookiejar.py 98.08% <66.66%> (-0.75%) ⬇️

📣 Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today!

@Dreamsorcerer Dreamsorcerer merged commit 85ec0e4 into aio-libs:3.9 Nov 12, 2023
25 of 31 checks passed
xiangxli pushed a commit to xiangxli/aiohttp that referenced this pull request Dec 4, 2023
…` when the jar is empty or all cookies have expired (aio-libs#7822)

**This is a backport of PR aio-libs#7819 as merged into master (dfc3f89).**

<!-- Thank you for your contribution! -->

The filtering itself and its preparation in `CookieJar.filter_cookies()`
is expensive. Sometimes there are no cookies in the jar or all cookies
have expired. Skip filtering and its preparation in this case.

Because the empty check is much cheaper than `_do_expiration()`, I think
it deserves to be duplicated before and after calling
`_do_expiration()`.

```console
$ python3.11 -m timeit -s 'from collections import defaultdict; d=defaultdict(foo="bar")' \
> 'if not d: pass'
50000000 loops, best of 5: 8.3 nsec per loop
$ python3.11 -m timeit -s 'from collections import defaultdict; d=defaultdict()' \
> 'if not d: pass'
50000000 loops, best of 5: 8.74 nsec per loop
$ python3.11 -m timeit -s 'from aiohttp import CookieJar; cj = CookieJar()' \
> 'cj._do_expiration()'
200000 loops, best of 5: 1.86 usec per loop
```

<!-- Please give a short brief about these changes. -->

No.

<!-- Outline any notable behaviour for the end users. -->

aio-libs#7583 (comment)

<!-- Are there any issues opened that will be resolved by merging this
change? -->

- [x] I think the code is well written
- [ ] Unit tests for the changes exist
- [ ] Documentation reflects the changes
- [x] If you provide code modification, please add yourself to
`CONTRIBUTORS.txt`
  * The format is &lt;Name&gt; &lt;Surname&gt;.
  * Please keep alphabetical order, the file is sorted by names.
- [x] Add a new news fragment into the `CHANGES` folder
  * name it `<issue_id>.<type>` for example (588.bugfix)
* if you don't have an `issue_id` change it to the pr id after creating
the pr
  * ensure type is one of the following:
    * `.feature`: Signifying a new feature.
    * `.bugfix`: Signifying a bug fix.
    * `.doc`: Signifying a documentation improvement.
    * `.removal`: Signifying a deprecation or removal of public API.
* `.misc`: A ticket has been closed, but it is not of interest to users.
* Make sure to use full sentences with correct case and punctuation, for
example: "Fix issue with non-ascii contents in doctest text files."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants