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

Implement heapq for cookie expire times #9203

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft

Implement heapq for cookie expire times #9203

wants to merge 4 commits into from

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Sep 19, 2024

What do these changes do?

Implement heapq for cookie expire times

Are there changes in behavior for the user?

no

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

heapq is a bit of a complex structure to maintain

Related issue number

fixes #8575
fixes #7790

note that profile is not exactly the same number of iterations since its using a real world use case of 60s and its hard to get it exactly the same (1644 before) / (1413 after) but its such a significant difference, its more than enough to show the performance improvement of ~96-97% less run time (adjusted ratio for the iterations):

before
do_expiration_before

after
do_expiration_after

# expired heap, rebuild the expired heap without the deleted items
# and clear the deleted items.
deleted_len = len(self._deleted)
if deleted_len > 100 and deleted_len > len(self._expire_heap) * 2:
Copy link
Member Author

Choose a reason for hiding this comment

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

probably should make 100 a constant MIN_DELETED_TO_CLEANUP

@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 19, 2024
Copy link

codecov bot commented Sep 19, 2024

Codecov Report

Attention: Patch coverage is 83.87097% with 5 lines in your changes missing coverage. Please review.

Project coverage is 98.30%. Comparing base (88f3834) to head (30cc307).

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
aiohttp/cookiejar.py 83.87% 2 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9203      +/-   ##
==========================================
- Coverage   98.31%   98.30%   -0.02%     
==========================================
  Files         107      107              
  Lines       34508    34520      +12     
  Branches     4100     4104       +4     
==========================================
+ Hits        33928    33935       +7     
- Misses        410      412       +2     
- Partials      170      173       +3     
Flag Coverage Δ
CI-GHA 98.20% <83.87%> (-0.02%) ⬇️
OS-Linux 97.86% <83.87%> (-0.02%) ⬇️
OS-Windows 96.28% <83.87%> (-0.02%) ⬇️
OS-macOS 97.54% <83.87%> (-0.02%) ⬇️
Py-3.10.11 97.64% <83.87%> (-0.02%) ⬇️
Py-3.10.15 97.57% <83.87%> (-0.02%) ⬇️
Py-3.11.10 97.46% <83.87%> (-0.02%) ⬇️
Py-3.11.9 97.52% <83.87%> (-0.02%) ⬇️
Py-3.12.6 97.91% <83.87%> (-0.02%) ⬇️
Py-3.9.13 97.52% <83.87%> (-0.02%) ⬇️
Py-3.9.20 97.46% <83.87%> (-0.02%) ⬇️
Py-pypy7.3.16 97.08% <83.87%> (-0.02%) ⬇️
VM-macos 97.54% <83.87%> (-0.02%) ⬇️
VM-ubuntu 97.86% <83.87%> (-0.02%) ⬇️
VM-windows 96.28% <83.87%> (-0.02%) ⬇️

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 added a commit that referenced this pull request Sep 19, 2024
noticed while working on #9203

TODO: show profiles
bdraco added a commit that referenced this pull request Sep 19, 2024
noticed while working on #9203

TODO: show profiles
break
heapq.heappop(self._expire_heap)
cookie_key = entry[1:]
if self._expirations.get(cookie_key) == when:
Copy link
Member Author

Choose a reason for hiding this comment

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

needs some tests to changing expire times

Going backwards, going forwards, and the same time seen twice

if not expire_heap_len:
return

if expire_heap_len > 100 and expire_heap_len > len(self._expirations) * 2:
Copy link
Member Author

Choose a reason for hiding this comment

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

Need a test for the cleanup

)
or predicate(morsel)
]
if to_del:
Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like we don't have coverage for clear matching nothing

@@ -167,11 +159,40 @@ def __len__(self) -> int:
return sum(len(cookie.values()) for cookie in self._cookies.values())

def _do_expiration(self) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

I'd put some comments in this function, it's a little dense to make sense of.

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expiring the cookie jar is unexpectedly expensive Remove _next_expiration field from CookieJar
2 participants