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 parsing of the content-type #10552

Merged
merged 6 commits into from
Mar 15, 2025
Merged

Cache parsing of the content-type #10552

merged 6 commits into from
Mar 15, 2025

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Mar 15, 2025

What do these changes do?

When profiling some frequent POST requests, I found the bulk of the time was spent parsing the content-type string. Use the same strategy as we do for parse_mimetype to cache the parsing.

Are there changes in behavior for the user?

performance improvement

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

no

Related issue number

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_or_pr_num>.<type>.rst (e.g. 588.bugfix.rst)

    • if you don't have an issue number, change it to the pull request
      number after creating the PR

      • .bugfix: A bug fix for something the maintainers deemed an
        improper undesired behavior that got corrected to match
        pre-agreed expectations.
      • .feature: A new behavior, public APIs. That sort of stuff.
      • .deprecation: A declaration of future API removals and breaking
        changes in behavior.
      • .breaking: When something public is removed in a breaking way.
        Could be deprecated in an earlier release.
      • .doc: Notable updates to the documentation structure or build
        process.
      • .packaging: Notes for downstreams about unobvious side effects
        and tooling. Changes in the test invocation considerations and
        runtime assumptions.
      • .contrib: Stuff that affects the contributor experience. e.g.
        Running tests, building the docs, setting up the development
        environment.
      • .misc: Changes that are hard to assign to any of the above
        categories.
    • Make sure to use full sentences with correct case and punctuation,
      for example:

      Fixed issue with non-ascii contents in doctest text files
      -- by :user:`contributor-gh-handle`.

      Use the past tense or the present tense a non-imperative mood,
      referring to what's changed compared to the last released version
      of this project.

Screenshot 2025-03-15 at 11 25 10 AM

Sorry, something went wrong.

When profiling some frequent POST requests, I found the bulk of
the time was spent parsing the content-type string. Use the
same strategy as we do for parse_mimetype to cache
the parsing.
@bdraco bdraco added backport-3.11 Trigger automatic backporting to the 3.11 release branch by Patchback robot backport-3.12 Trigger automatic backporting to the 3.12 release branch by Patchback robot labels Mar 15, 2025
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Mar 15, 2025
Copy link

codecov bot commented Mar 15, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.70%. Comparing base (55c5f1f) to head (0369a2a).
Report is 1 commits behind head on master.

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #10552   +/-   ##
=======================================
  Coverage   98.69%   98.70%           
=======================================
  Files         122      122           
  Lines       37246    37252    +6     
  Branches     2065     2065           
=======================================
+ Hits        36761    36770    +9     
+ Misses        338      335    -3     
  Partials      147      147           
Flag Coverage Δ
CI-GHA 98.58% <100.00%> (+<0.01%) ⬆️
OS-Linux 98.25% <100.00%> (+<0.01%) ⬆️
OS-Windows 96.18% <100.00%> (+<0.01%) ⬆️
OS-macOS 97.36% <100.00%> (+<0.01%) ⬆️
Py-3.10.11 97.27% <100.00%> (+<0.01%) ⬆️
Py-3.10.16 97.81% <100.00%> (+<0.01%) ⬆️
Py-3.11.11 97.90% <100.00%> (+<0.01%) ⬆️
Py-3.11.9 97.35% <100.00%> (+<0.01%) ⬆️
Py-3.12.9 98.35% <100.00%> (+<0.01%) ⬆️
Py-3.13.2 98.34% <100.00%> (+<0.01%) ⬆️
Py-3.9.13 97.15% <100.00%> (+<0.01%) ⬆️
Py-3.9.21 97.68% <100.00%> (+<0.01%) ⬆️
Py-pypy7.3.16 83.36% <100.00%> (-3.31%) ⬇️
VM-macos 97.36% <100.00%> (+<0.01%) ⬆️
VM-ubuntu 98.25% <100.00%> (+<0.01%) ⬆️
VM-windows 96.18% <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.

Copy link

codspeed-hq bot commented Mar 15, 2025

CodSpeed Performance Report

Merging #10552 will improve performances by 15.48%

Comparing cache_content_type_parse (0369a2a) with master (55c5f1f)

Summary

⚡ 1 improvements
✅ 46 untouched benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
test_one_hundred_json_post_requests[pyloop] 44.7 ms 38.7 ms +15.48%

bdraco added 2 commits March 15, 2025 11:58
@bdraco
Copy link
Member Author

bdraco commented Mar 15, 2025

Merging #10552 will improve performances by 15.48%

Since the benchmark is benchmarking both sides of the connection and has some overhead on its own, the real world improvement should be around ~30% (or more depending on the I/O overhead)

Screenshot 2025-03-15 at 12 36 02 PM

@bdraco bdraco marked this pull request as ready for review March 15, 2025 22:36
@bdraco bdraco merged commit 44e669b into master Mar 15, 2025
40 checks passed
@bdraco bdraco deleted the cache_content_type_parse branch March 15, 2025 22:56
Copy link
Contributor

patchback bot commented Mar 15, 2025

Backport to 3.11: 💚 backport PR created

✅ Backport PR branch: patchback/backports/3.11/44e669be1ab1a60c40183f92f172670d912cb834/pr-10552

Backported as #10557

🤖 @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 Mar 15, 2025
(cherry picked from commit 44e669b)
Copy link
Contributor

patchback bot commented Mar 15, 2025

Backport to 3.12: 💚 backport PR created

✅ Backport PR branch: patchback/backports/3.12/44e669be1ab1a60c40183f92f172670d912cb834/pr-10552

Backported as #10558

🤖 @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 Mar 15, 2025
(cherry picked from commit 44e669b)
bdraco added a commit that referenced this pull request Mar 15, 2025
…#10557)

**This is a backport of PR #10552 as merged into master
(44e669b).**



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

## What do these changes do?

When profiling some frequent POST requests, I found the bulk of the time
was spent parsing the content-type string. Use the same strategy as we
do for `parse_mimetype` to cache the parsing.

## Are there changes in behavior for the user?

performance improvement

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

no

## Related issue number

<!-- Are there any issues opened that will be resolved by merging this
change? -->
<!-- Remember to prefix with 'Fixes' if it should close the issue (e.g.
'Fixes #123'). -->

## Checklist

- [x] 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 &lt;Name&gt; &lt;Surname&gt;.
  * Please keep alphabetical order, the file is sorted by names.
- [ ] Add a new news fragment into the `CHANGES/` folder
  * name it `<issue_or_pr_num>.<type>.rst` (e.g. `588.bugfix.rst`)
  * if you don't have an issue number, change it to the pull request
    number after creating the PR
    * `.bugfix`: A bug fix for something the maintainers deemed an
      improper undesired behavior that got corrected to match
      pre-agreed expectations.
    * `.feature`: A new behavior, public APIs. That sort of stuff.
    * `.deprecation`: A declaration of future API removals and breaking
      changes in behavior.
    * `.breaking`: When something public is removed in a breaking way.
      Could be deprecated in an earlier release.
    * `.doc`: Notable updates to the documentation structure or build
      process.
    * `.packaging`: Notes for downstreams about unobvious side effects
      and tooling. Changes in the test invocation considerations and
      runtime assumptions.
    * `.contrib`: Stuff that affects the contributor experience. e.g.
      Running tests, building the docs, setting up the development
      environment.
    * `.misc`: Changes that are hard to assign to any of the above
      categories.
  * Make sure to use full sentences with correct case and punctuation,
    for example:
    ```rst
    Fixed issue with non-ascii contents in doctest text files
    -- by :user:`contributor-gh-handle`.
    ```

    Use the past tense or the present tense a non-imperative mood,
    referring to what's changed compared to the last released version
    of this project.

<img width="570" alt="Screenshot 2025-03-15 at 11 25 10 AM"
src="https://github.com/user-attachments/assets/cabaaa7c-3a39-4f90-b450-a6a0559d22d6"
/>

Co-authored-by: J. Nick Koston <nick@koston.org>
bdraco added a commit that referenced this pull request Mar 15, 2025
…#10558)

**This is a backport of PR #10552 as merged into master
(44e669b).**



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

## What do these changes do?

When profiling some frequent POST requests, I found the bulk of the time
was spent parsing the content-type string. Use the same strategy as we
do for `parse_mimetype` to cache the parsing.

## Are there changes in behavior for the user?

performance improvement

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

no

## Related issue number

<!-- Are there any issues opened that will be resolved by merging this
change? -->
<!-- Remember to prefix with 'Fixes' if it should close the issue (e.g.
'Fixes #123'). -->

## Checklist

- [x] 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 &lt;Name&gt; &lt;Surname&gt;.
  * Please keep alphabetical order, the file is sorted by names.
- [ ] Add a new news fragment into the `CHANGES/` folder
  * name it `<issue_or_pr_num>.<type>.rst` (e.g. `588.bugfix.rst`)
  * if you don't have an issue number, change it to the pull request
    number after creating the PR
    * `.bugfix`: A bug fix for something the maintainers deemed an
      improper undesired behavior that got corrected to match
      pre-agreed expectations.
    * `.feature`: A new behavior, public APIs. That sort of stuff.
    * `.deprecation`: A declaration of future API removals and breaking
      changes in behavior.
    * `.breaking`: When something public is removed in a breaking way.
      Could be deprecated in an earlier release.
    * `.doc`: Notable updates to the documentation structure or build
      process.
    * `.packaging`: Notes for downstreams about unobvious side effects
      and tooling. Changes in the test invocation considerations and
      runtime assumptions.
    * `.contrib`: Stuff that affects the contributor experience. e.g.
      Running tests, building the docs, setting up the development
      environment.
    * `.misc`: Changes that are hard to assign to any of the above
      categories.
  * Make sure to use full sentences with correct case and punctuation,
    for example:
    ```rst
    Fixed issue with non-ascii contents in doctest text files
    -- by :user:`contributor-gh-handle`.
    ```

    Use the past tense or the present tense a non-imperative mood,
    referring to what's changed compared to the last released version
    of this project.

<img width="570" alt="Screenshot 2025-03-15 at 11 25 10 AM"
src="https://github.com/user-attachments/assets/cabaaa7c-3a39-4f90-b450-a6a0559d22d6"
/>

Co-authored-by: J. Nick Koston <nick@koston.org>
@bdraco
Copy link
Member Author

bdraco commented Mar 16, 2025

checked 60s HA run.

cache size is effective
2025-03-16 13:40:51.524 CRITICAL (SyncWorker_2) [homeassistant.components.profiler] Cache stats for lru_cache <function parse_content_type at 0x7f5324f356c0> at /usr/local/lib/python3.13/site-packages/aiohttp/helpers.py: CacheInfo(hits=168, misses=6, maxsize=56, currsize=6)

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 backport-3.12 Trigger automatic backporting to the 3.12 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.

None yet

2 participants