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

Cache construction of middleware handlers #9158

Merged
merged 7 commits into from
Sep 17, 2024

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Sep 16, 2024

What do these changes do?

Cache construction of middleware. Calling update_wrapper many times for each request with middleware is more expensive than running all the middlewares

The update_wrapper call was introduced in #4195 which is what made middleware much slower.

Are there changes in behavior for the user?

No

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

no

Before

bdraco@MacBook-Pro-5 ~ % wrk -t 10 http://localhost:8123
Running 10s test @ http://localhost:8123
  10 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     6.83ms   33.14ms 348.84ms   96.25%
    Req/Sec     1.89k   799.64     2.41k    80.83%
  181206 requests in 10.01s, 0.95GB read
Requests/sec:  18100.19
Transfer/sec:     97.39MB

After

bdraco@MacBook-Pro-5 ~ % wrk -t 10 http://localhost:8123
Running 10s test @ http://localhost:8123
  10 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   386.34us  124.22us   5.11ms   98.73%
    Req/Sec     2.63k   156.95     2.77k    96.04%
  264316 requests in 10.10s, 1.39GB read
Requests/sec:  26169.50
Transfer/sec:    140.81MB

middleware

@bdraco bdraco added backport-3.10 Trigger automatic backporting to the 3.10 release branch by Patchback robot backport-3.11 Trigger automatic backporting to the 3.11 release branch by Patchback robot labels Sep 16, 2024
aiohttp/web_app.py Outdated Show resolved Hide resolved
Copy link

codecov bot commented Sep 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.31%. Comparing base (fa98921) to head (0f6c10a).
Report is 4 commits behind head on master.

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9158   +/-   ##
=======================================
  Coverage   98.31%   98.31%           
=======================================
  Files         107      107           
  Lines       34461    34469    +8     
  Branches     4089     4092    +3     
=======================================
+ Hits        33879    33887    +8     
  Misses        411      411           
  Partials      171      171           
Flag Coverage Δ
CI-GHA 98.20% <100.00%> (+<0.01%) ⬆️
OS-Linux 97.87% <100.00%> (+<0.01%) ⬆️
OS-Windows 96.28% <100.00%> (+<0.01%) ⬆️
OS-macOS 97.54% <100.00%> (+<0.01%) ⬆️
Py-3.10.11 97.64% <100.00%> (+<0.01%) ⬆️
Py-3.10.14 97.58% <100.00%> (+<0.01%) ⬆️
Py-3.11.9 97.80% <100.00%> (+<0.01%) ⬆️
Py-3.12.5 97.58% <100.00%> (+<0.01%) ⬆️
Py-3.12.6 97.64% <100.00%> (+<0.01%) ⬆️
Py-3.9.13 97.53% <100.00%> (+<0.01%) ⬆️
Py-3.9.19 97.47% <100.00%> (+0.09%) ⬆️
Py-3.9.20 ?
Py-pypy7.3.16 97.08% <100.00%> (+<0.01%) ⬆️
VM-macos 97.54% <100.00%> (+<0.01%) ⬆️
VM-ubuntu 97.87% <100.00%> (+<0.01%) ⬆️
VM-windows 96.28% <100.00%> (+<0.01%) ⬆️

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.

aiohttp/web_app.py Outdated Show resolved Hide resolved
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Sep 16, 2024
@bdraco
Copy link
Member Author

bdraco commented Sep 16, 2024

Thanks for the quick look. Leaving now so I'll try to finish up later this week before I get back from holiday.

Copy link

@elupus elupus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesnt the cache need to be invalidated if an additional middleware is added at some late stage? (Know too little about internals to know if this can happen)

@Dreamsorcerer
Copy link
Member

Doesnt the cache need to be invalidated if an additional middleware is added at some late stage? (Know too little about internals to know if this can happen)

They're frozen, they can't be modified once the app is started.

@bdraco
Copy link
Member Author

bdraco commented Sep 17, 2024

Need to look at the lru stats once I have stable internet

@bdraco
Copy link
Member Author

bdraco commented Sep 17, 2024

2024-09-17 10:07:54.640 CRITICAL (SyncWorker_7) [homeassistant.components.profiler] Cache stats for lru_cache <function _build_middlewares at 0x7f9e8cc07600> at /usr/local/lib/python3.12/site-packages/aiohttp/web_app.py: CacheInfo(hits=15458, misses=19, maxsize=None, currsize=19)

@bdraco bdraco marked this pull request as ready for review September 17, 2024 15:21
@bdraco bdraco merged commit bf022b3 into master Sep 17, 2024
35 of 36 checks passed
@bdraco bdraco deleted the forward_port_middlewares_cache branch September 17, 2024 15:22
Copy link
Contributor

patchback bot commented Sep 17, 2024

Backport to 3.10: 💔 cherry-picking failed — conflicts found

❌ Failed to cleanly apply bf022b3 on top of patchback/backports/3.10/bf022b390ee97172d20c0f554b87868d0ef4d938/pr-9158

Backporting merged PR #9158 into master

  1. Ensure you have a local repo clone of your fork. Unless you cloned it
    from the upstream, this would be your origin remote.
  2. Make sure you have an upstream repo added as a remote too. In these
    instructions you'll refer to it by the name upstream. If you don't
    have it, here's how you can add it:
    $ git remote add upstream https://github.com/aio-libs/aiohttp.git
  3. Ensure you have the latest copy of upstream and prepare a branch
    that will hold the backported code:
    $ git fetch upstream
    $ git checkout -b patchback/backports/3.10/bf022b390ee97172d20c0f554b87868d0ef4d938/pr-9158 upstream/3.10
  4. Now, cherry-pick PR Cache construction of middleware handlers #9158 contents into that branch:
    $ git cherry-pick -x bf022b390ee97172d20c0f554b87868d0ef4d938
    If it'll yell at you with something like fatal: Commit bf022b390ee97172d20c0f554b87868d0ef4d938 is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x bf022b390ee97172d20c0f554b87868d0ef4d938
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR Cache construction of middleware handlers #9158 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.10/bf022b390ee97172d20c0f554b87868d0ef4d938/pr-9158
  7. Create a PR, ensure that the CI is green. If it's not — update it so that
    the tests and any other checks pass. This is it!
    Now relax and wait for the maintainers to process your pull request
    when they have some cycles to do reviews. Don't worry — they'll tell you if
    any improvements are necessary when the time comes!

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

Copy link
Contributor

patchback bot commented Sep 17, 2024

Backport to 3.11: 💔 cherry-picking failed — conflicts found

❌ Failed to cleanly apply bf022b3 on top of patchback/backports/3.11/bf022b390ee97172d20c0f554b87868d0ef4d938/pr-9158

Backporting merged PR #9158 into master

  1. Ensure you have a local repo clone of your fork. Unless you cloned it
    from the upstream, this would be your origin remote.
  2. Make sure you have an upstream repo added as a remote too. In these
    instructions you'll refer to it by the name upstream. If you don't
    have it, here's how you can add it:
    $ git remote add upstream https://github.com/aio-libs/aiohttp.git
  3. Ensure you have the latest copy of upstream and prepare a branch
    that will hold the backported code:
    $ git fetch upstream
    $ git checkout -b patchback/backports/3.11/bf022b390ee97172d20c0f554b87868d0ef4d938/pr-9158 upstream/3.11
  4. Now, cherry-pick PR Cache construction of middleware handlers #9158 contents into that branch:
    $ git cherry-pick -x bf022b390ee97172d20c0f554b87868d0ef4d938
    If it'll yell at you with something like fatal: Commit bf022b390ee97172d20c0f554b87868d0ef4d938 is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x bf022b390ee97172d20c0f554b87868d0ef4d938
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR Cache construction of middleware handlers #9158 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.11/bf022b390ee97172d20c0f554b87868d0ef4d938/pr-9158
  7. Create a PR, ensure that the CI is green. If it's not — update it so that
    the tests and any other checks pass. This is it!
    Now relax and wait for the maintainers to process your pull request
    when they have some cycles to do reviews. Don't worry — they'll tell you if
    any improvements are necessary when the time comes!

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

bdraco added a commit that referenced this pull request Sep 17, 2024
bdraco added a commit that referenced this pull request Sep 17, 2024
bdraco added a commit that referenced this pull request Sep 18, 2024
In post merge self re-review of #9158, I noticed I
should have moved the reversing into the cache
bdraco added a commit that referenced this pull request Sep 18, 2024
In post merge self re-review of #9158, I noticed I
should have moved the reversing into the cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-3.10 Trigger automatic backporting to the 3.10 release branch by Patchback robot 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.

3 participants