X Tutup
The Wayback Machine - https://web.archive.org/web/20230605041044/https://github.com/python/cpython/pull/103208
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

gh-103200: Fix performance issues with zipimport.invalidate_caches() #103208

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

desmondcheongzx
Copy link
Contributor

@desmondcheongzx desmondcheongzx commented Apr 3, 2023

This PR fixes the over-eagerness of the original zipimport.invalidate_caches() implementation.

Currently in zipimport.invalidate_caches(), the cache of zip files is repopulated at the point of invalidation. This causes cache invalidation to be slow, and violates the semantics of cache invalidation which should simply clear the cache. Cache repopulation should occur on the next access of files.

There are three relevant events to consider:

  1. The cache is accessed while valid
  2. invalidate_caches() is called
  3. The cache is accessed after being invalidated

Events (1) and (2) should be fast, while event (3) can be slow since we're repopulating a cache. In the original implementation, (1) and (3) are fast, but (2) is slow.

This PR shifts the cost of reading the directory out of cache invalidation and back to cache access, while avoiding any behaviour change introduced in Python 3.10+ and keeping the common path of reading the cache performant.

Ideally, this fix should be backported to Python 3.10+.

@cpython-cla-bot
Copy link

cpython-cla-bot bot commented Apr 3, 2023

All commit authors signed the Contributor License Agreement.
CLA signed

@desmondcheongzx desmondcheongzx force-pushed the fix-zipimport-invalidate-caches branch from 2618585 to e01f4f6 Compare April 3, 2023 08:03
@desmondcheongzx desmondcheongzx force-pushed the fix-zipimport-invalidate-caches branch 2 times, most recently from a9d791a to 3ed86c0 Compare April 3, 2023 08:27
@desmondcheongzx desmondcheongzx force-pushed the fix-zipimport-invalidate-caches branch from 3ed86c0 to e2bd85a Compare April 3, 2023 08:30
@arhadthedev arhadthedev added performance Performance or resource usage stdlib Python modules in the Lib dir labels Apr 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review performance Performance or resource usage stdlib Python modules in the Lib dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants
X Tutup