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

Significantly speed up filter_cookies #9204

Merged
merged 17 commits into from
Sep 22, 2024
Merged

Significantly speed up filter_cookies #9204

merged 17 commits into from
Sep 22, 2024

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Sep 19, 2024

What do these changes do?

Significantly speed up filter_cookies by adding a cache for morsel construction. Previously every request had to create new Morsel objects. Its a common pattern to set a cookie once and read it many times. When testing with Home Assistant, the cache had nearly a 100% hit rate.

Benchmark #7790 (comment)

before
filter_domain: 6.765125583391637

after
filter_domain: 1.3521433752030134

Are there changes in behavior for the user?

no

Is it a substantial burden for the maintainers to support this?

no

noticed while working on #9203

TODO: show profiles
@bdraco bdraco added backport-3.10 backport-3.11 Trigger automatic backporting to the 3.11 release branch by Patchback robot labels Sep 19, 2024
Copy link

codecov bot commented Sep 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.42%. Comparing base (e079c41) to head (efd02ba).
Report is 1056 commits behind head on master.

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9204   +/-   ##
=======================================
  Coverage   98.42%   98.42%           
=======================================
  Files         107      107           
  Lines       34654    34708   +54     
  Branches     4104     4113    +9     
=======================================
+ Hits        34107    34161   +54     
  Misses        380      380           
  Partials      167      167           
Flag Coverage Δ
CI-GHA 98.31% <100.00%> (+<0.01%) ⬆️
OS-Linux 97.98% <100.00%> (+<0.01%) ⬆️
OS-Windows 96.37% <100.00%> (-0.04%) ⬇️
OS-macOS 97.66% <100.00%> (+<0.01%) ⬆️
Py-3.10.11 97.75% <100.00%> (+<0.01%) ⬆️
Py-3.10.15 97.69% <100.00%> (+<0.01%) ⬆️
Py-3.11.10 97.58% <100.00%> (+<0.01%) ⬆️
Py-3.11.9 97.65% <100.00%> (+<0.01%) ⬆️
Py-3.12.6 98.03% <100.00%> (+<0.01%) ⬆️
Py-3.9.13 97.64% <100.00%> (+<0.01%) ⬆️
Py-3.9.20 97.58% <100.00%> (+<0.01%) ⬆️
Py-pypy7.3.16 97.20% <100.00%> (+<0.01%) ⬆️
VM-macos 97.66% <100.00%> (+<0.01%) ⬆️
VM-ubuntu 97.98% <100.00%> (+<0.01%) ⬆️
VM-windows 96.37% <100.00%> (-0.04%) ⬇️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Dreamsorcerer
Copy link
Member

This doesn't look like it should really make a difference.. I feel like the extra nesting might hurt readability too.

@bdraco
Copy link
Member Author

bdraco commented Sep 20, 2024

Will do a more detailed profile later. If its not significant, I'll close this as it won't be churn if its only small.

@bdraco
Copy link
Member Author

bdraco commented Sep 21, 2024

#7790 (comment)

This benchmark should work

@bdraco
Copy link
Member Author

bdraco commented Sep 21, 2024

before

% python3 filter_large_cookie_jar.py
filter_domain: 6.765125583391637
filter_other_domain: 0.0018160408362746239

@bdraco
Copy link
Member Author

bdraco commented Sep 21, 2024

after

filter_domain: 6.75009624985978
filter_other_domain: 0.0017206249758601189

@bdraco
Copy link
Member Author

bdraco commented Sep 21, 2024

Based on that I'd say its not worth the churn

@bdraco bdraco closed this Sep 21, 2024
@bdraco
Copy link
Member Author

bdraco commented Sep 21, 2024

Still need to investigate if there is anything we can do to improve the filter domain case but that seems like it won't be a small change. I'm not sure if it's still a problem though

@bdraco bdraco reopened this Sep 21, 2024
@bdraco bdraco changed the title Small speed up to filter_cookies Significantly speed up filter_cookies Sep 21, 2024
@bdraco
Copy link
Member Author

bdraco commented Sep 21, 2024

Did some more analysis and found out that most of the time is copying the cookie and making another Morsel object so I added a cache since most of the time we set once, ready many.

With adding a cache

filter_domain: 1.3521433752030134

@bdraco
Copy link
Member Author

bdraco commented Sep 21, 2024

Still in flight for a few more hours so will need to do some real world analysis to make sure the cache is effective as expected.

Right now we spend more time in cookie.py/cookiejar.py than http_writer.py in most cases so it would be a really nice speed up if we can make filter_cookies faster

@bdraco
Copy link
Member Author

bdraco commented Sep 21, 2024

The hit rate on the cache is near 100% at least on Home Assistant so it definitely seems worth caching

before
filter_cookies_before

after
filter_cookies_after

On another system
filter_cookies_after_other

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Sep 22, 2024
@bdraco bdraco marked this pull request as ready for review September 22, 2024 15:53
aiohttp/cookiejar.py Outdated Show resolved Hide resolved
@bdraco bdraco enabled auto-merge (squash) September 22, 2024 17:18
@bdraco bdraco merged commit 34a626d into master Sep 22, 2024
31 of 32 checks passed
@bdraco bdraco deleted the speed_up_filter_cookies branch September 22, 2024 19:37
Copy link
Contributor

patchback bot commented Sep 22, 2024

Backport to 3.10: 💚 backport PR created

✅ Backport PR branch: patchback/backports/3.10/34a626dad03a013766c668a8db59d53c036177dd/pr-9204

Backported as #9243

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback bot pushed a commit that referenced this pull request Sep 22, 2024
Copy link
Contributor

patchback bot commented Sep 22, 2024

Backport to 3.11: 💚 backport PR created

✅ Backport PR branch: patchback/backports/3.11/34a626dad03a013766c668a8db59d53c036177dd/pr-9204

Backported as #9244

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback bot pushed a commit that referenced this pull request Sep 22, 2024
bdraco added a commit that referenced this pull request Sep 22, 2024
…ies (#9243)

Co-authored-by: J. Nick Koston <nick@koston.org>
bdraco added a commit that referenced this pull request Sep 22, 2024
…ies (#9244)

Co-authored-by: J. Nick Koston <nick@koston.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-3.11 Trigger automatic backporting to the 3.11 release branch by Patchback robot 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