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

Speed up the ConnectionKey #9365

Merged
merged 5 commits into from
Oct 1, 2024
Merged

Speed up the ConnectionKey #9365

merged 5 commits into from
Oct 1, 2024

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Oct 1, 2024

A large part of the connection management is constructing, hashing and comparing the connection key. I noticed this when I was profiling and looking at py-spy output to compare which code paths aiohappyeyeballs took on master and 3.10

For additional details see #9364

To speed this up, ConnectionKey is now a NamedTuple which allows all the __hash__ and __eq__ operations to happen in native code.

https://github.com/python/cpython/blob/133e929a791d209b578b4822a7a07f4570b3803b/Objects/tupleobject.c#L319

The construction of the object is a bit faster by calling the yarl.URL methods directly.

I didn't find any third party code that was altering modifying connection_key https://github.com/search?q=req.connection_key&type=code so its likely this change is safe to backport to 3.10 but I was a bit worried about custom proxy Connectors (although that seems unlikely) so I marked it as breaking and only labeled it for 3.11.

What do these changes do?

Speed up connections

Are there changes in behavior for the user?

The ConnectionKey is now a NamedTuple

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

no

fixes #9364

A large part of the connection management is constructing, hashing
and comparing the connection key

For additional details see #9364

To speed this up, ConnectionKey is now a NamedTuple which allows all
the __hash__ and __eq__ operations to happen in native code.

https://github.com/python/cpython/blob/133e929a791d209b578b4822a7a07f4570b3803b/Objects/tupleobject.c#L319

The construction of the object is a bit faster by calling the
yarl URL methods directly
@bdraco bdraco added the backport-3.11 Trigger automatic backporting to the 3.11 release branch by Patchback robot label Oct 1, 2024
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Oct 1, 2024
Copy link

codecov bot commented Oct 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.56%. Comparing base (c25099c) to head (dd9de54).
Report is 720 commits behind head on master.

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9365      +/-   ##
==========================================
+ Coverage   98.07%   98.56%   +0.48%     
==========================================
  Files         105      107       +2     
  Lines       34908    34976      +68     
  Branches     4116     4144      +28     
==========================================
+ Hits        34237    34473     +236     
+ Misses        466      335     -131     
+ Partials      205      168      -37     
Flag Coverage Δ
CI-GHA 98.44% <100.00%> (+0.44%) ⬆️
OS-Linux 98.10% <100.00%> (?)
OS-Windows 96.51% <100.00%> (+0.43%) ⬆️
OS-macOS 97.79% <100.00%> (+0.22%) ⬆️
Py-3.10.11 97.66% <100.00%> (+1.58%) ⬆️
Py-3.10.15 97.60% <100.00%> (?)
Py-3.11.10 97.66% <100.00%> (?)
Py-3.11.9 97.74% <100.00%> (?)
Py-3.12.6 98.15% <100.00%> (+0.59%) ⬆️
Py-3.13.0-rc.2 98.14% <100.00%> (?)
Py-3.9.13 97.56% <100.00%> (?)
Py-3.9.20 97.50% <100.00%> (?)
Py-pypy7.3.16 97.12% <100.00%> (?)
VM-macos 97.79% <100.00%> (+0.22%) ⬆️
VM-ubuntu 98.10% <100.00%> (?)
VM-windows 96.51% <100.00%> (+0.43%) ⬆️

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.

@bdraco
Copy link
Member Author

bdraco commented Oct 1, 2024

Here is what one of attrs ConnectionKey dict gets looks like (lots of __hash__ and __eq__)
Screenshot 2024-10-01 at 9 51 43 AM

@bdraco bdraco marked this pull request as ready for review October 1, 2024 17:02
@bdraco bdraco merged commit d684195 into master Oct 1, 2024
38 of 39 checks passed
@bdraco bdraco deleted the speed_up_connection_key branch October 1, 2024 17:02
Copy link
Contributor

patchback bot commented Oct 1, 2024

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

❌ Failed to cleanly apply d684195 on top of patchback/backports/3.11/d684195bb5cb90a85a07b98127a298f3b1ae2ef3/pr-9365

Backporting merged PR #9365 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/d684195bb5cb90a85a07b98127a298f3b1ae2ef3/pr-9365 upstream/3.11
  4. Now, cherry-pick PR Speed up the ConnectionKey #9365 contents into that branch:
    $ git cherry-pick -x d684195bb5cb90a85a07b98127a298f3b1ae2ef3
    If it'll yell at you with something like fatal: Commit d684195bb5cb90a85a07b98127a298f3b1ae2ef3 is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x d684195bb5cb90a85a07b98127a298f3b1ae2ef3
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR Speed up the ConnectionKey #9365 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.11/d684195bb5cb90a85a07b98127a298f3b1ae2ef3/pr-9365
  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 Oct 1, 2024
(cherry picked from commit d684195)
@bdraco
Copy link
Member Author

bdraco commented Oct 3, 2024

Screenshot 2024-10-03 at 6 12 56 PM

Ever with the cached hash on attrs, the eq shows up in the profile often in 3.10 still.
Will be super nice to get this in 3.11

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.

ConnectionKey hashing and equality has an unexpected impact on performance between master and 3.x
1 participant