Skip to content

Skip invalidate_caches() call on Python dedicated snapshot path - #8253

Merged
ryanking13 merged 1 commit into
mainfrom
gyeongjae/keep-zip-directory-cache
Nov 13, 2026
Merged

ryanking13 merged 1 commit into
mainfrom
gyeongjae/keep-zip-directory-cache

Conversation

@ryanking13

Copy link
Copy Markdown
Contributor

One more micro-optimization for cold start, similar to #8208.

invalidate_caches is used to clear up the Python importlib cache. This was used to clean up the cache that was populated from the baseline snapshot which is not a user worker. However, in dedicated snashot, the import cache is created from the user worker so we don't need to clean it up again.

Hopefully we should be able to save another ~10ms of cold start time with this.

@ryanking13
ryanking13 requested review from dom96 and hoodmane November 6, 2026 07:38
@ryanking13
ryanking13 requested review from a team as code owners November 6, 2026 07:38
Comment thread src/pyodide/internal/setupPackages.ts Resolved

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 4d907bd2.

This review, of 549a312c

✅ Approved — This is a small, correctly guarded optimization: dedicated snapshots skip the call, all other paths behave as before, and a test covers the change. No issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code Quality: ✅ LGTM

The call moved from mountWorkerFiles to loadPyodide and runs right after it, so the order is unchanged for workers with no snapshot and for baseline snapshots. The guards in snapshot.ts and python.ts agree, because LOADED_SNAPSHOT_TYPE comes from the loaded snapshot's settings.snapshotType. The extended subdirectory test covers the zip directory cache and imports of a worker file and a stdlib module.


📝 Documentation: ✅ LGTM

The new code comments match the current behaviour.


Reviewed commit: 549a312c

@ryanking13
ryanking13 force-pushed the gyeongjae/keep-zip-directory-cache branch from 549a312 to 4d907bd Compare November 6, 2026 08:21

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 5a3e63f1.

This review, of 4d907bd2

✅ Approved — Dedicated snapshots skip the cache invalidation, every other path behaves as before, and a test covers the change. No issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The invalidateCaches call moved out of mountWorkerFiles and now runs right after it in loadPyodide, so the order is unchanged for workers with no snapshot and for baseline snapshots. Both guards skip the call only for dedicated snapshots. The test changes are consistent: in BUILD.bazel the workflow-entrypoint tests have different names and use explicit src overrides, sdk.wd-test binds SERVER to the Server entrypoint, and the new subdir/b.py is registered in subdirectory.wd-test.


⚡ Performance: ✅ LGTM

Skipping the cache invalidation keeps the import caches built from the user worker, including the zip directory cache, so they are not rebuilt after a dedicated snapshot restore.


📝 Documentation: ✅ LGTM

The new code comments match the current behaviour.


Reviewed commit: 4d907bd2

@dom96 dom96 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I remember the invalidate_cache logic being tricky and we had to add a bunch of these in the past in places that didn't have them. This change makes sense to me, but I think we should get @hoodmane's thoughts on this too.

I also wonder if we should be more cautious (because this has a chance of breaking worker loading) and introduce this change behind a compat flag (or maybe just an autogate so we can quickly roll this back if it breaks things).

@ryanking13

Copy link
Copy Markdown
Contributor Author

I remember the invalidate_cache logic being tricky and we had to add a bunch of these in the past in places that didn't have them. This change makes sense to me, but I think we should get @hoodmane's thoughts on this too.
I also wonder if we should be more cautious (because this has a chance of breaking worker loading) and introduce this change behind a compat flag (or maybe just an autogate so we can quickly roll this back if it breaks things).

I see. Sure, putting this under the compat flag or autogate sounds reasonable. I don't think this is a breaking change for 99.9% cases, but users can do anything in Python...

@ryanking13
ryanking13 force-pushed the gyeongjae/keep-zip-directory-cache branch from 4d907bd to 5a3e63f Compare November 12, 2026 06:09

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 8c241596.

This review, of 5a3e63f1

✅ Approved — The change now sits behind an opt-in compatibility flag, the default path still invalidates caches as before, and tests cover both cases. No issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

  • Flag: pythonDedicatedSnapshotKeepImportCaches @206 uses the next free ordinal. It is read in metadata.ts and declared in metadata.d.ts. It reaches Python through the generic COMPATIBILITY_FLAGS object, so it needs no other wiring.
  • Default behaviour: KEEP_RESTORED_IMPORT_CACHES is true only for a dedicated snapshot with the flag set. In every other case invalidateCaches runs in maybeRestoreSnapshot and again after mountWorkerFiles, as it did before. mountWorkerFiles has only one caller, so no other path loses the call that moved out of it.
  • Tests: a.py checks that the zip directory cache is the same object after a dedicated restore when the flag is on, and a different object when it is off. The workflow-entrypoint and sdk test changes match the new .wd-test files. A TODO explains why the tests were split.

⚡ Performance: ✅ LGTM

With the flag set, a dedicated snapshot keeps the import caches it recorded, including the zip directory cache, so they are not rebuilt at cold start. Nothing new runs on the per-request path.


📝 Documentation: ✅ LGTM

The comment on the new compatibility flag and the code comments match the behaviour.


Reviewed commit: 5a3e63f1

@ryanking13
ryanking13 force-pushed the gyeongjae/keep-zip-directory-cache branch from 5a3e63f to 8c24159 Compare November 12, 2026 06:13

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of d8c97881.

This review, of 8c241596

✅ Approved — The optimization is behind an opt-in compatibility flag that has no default enable date, so the default path still invalidates caches. The flag uses the next free ordinal, and tests cover both settings. No issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The only change since the last review is the new flag in compatibility-date.capnp.

  • Ordinal: @206 is the next free ordinal, after exposeSerializationTestClass @205.
  • Opt-in: The flag has an enable name and a disable name, and no default enable date. It stays opt-in, so the default path still calls invalidateCaches.
  • Wiring: The generated COMPATIBILITY_FLAGS key python_dedicated_snapshot_keep_import_caches matches the name read in metadata.ts and declared in metadata.d.ts.
  • Tests: subdirectory-keep-import-caches.wd-test enables the flag, and a.py checks the behaviour both with the flag on and with it off.

⚡ Performance: ✅ LGTM

With the flag set, a dedicated snapshot keeps the import caches it restored, so they are not rebuilt at cold start. Every other path still invalidates caches as before, and nothing new runs per request.


📝 Documentation: ✅ LGTM

The comment on the new flag and the code comments match the behaviour.


Reviewed commit: 8c241596

@ryanking13
ryanking13 force-pushed the gyeongjae/keep-zip-directory-cache branch from 8c24159 to d8c9788 Compare November 13, 2026 08:34

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved — The optimization is behind an opt-in compatibility flag that has no default enable date, so the default path still invalidates caches. The flag uses the next free ordinal, and tests cover both settings. No issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The only change since the last review is the new flag in compatibility-date.capnp.

  • Ordinal: @206 is the next free ordinal, after exposeSerializationTestClass @205.
  • Opt-in: The flag has an enable name and a disable name, and no default enable date. It stays opt-in, so the default path still calls invalidateCaches.
  • Wiring: The generated COMPATIBILITY_FLAGS key python_dedicated_snapshot_keep_import_caches matches the name read in metadata.ts and declared in metadata.d.ts.
  • Tests: subdirectory-keep-import-caches.wd-test enables the flag, and a.py checks the behaviour both with the flag on and with it off.

⚡ Performance: ✅ LGTM

With the flag set, a dedicated snapshot keeps the import caches it restored, so they are not rebuilt at cold start. Every other path still invalidates caches as before, and nothing new runs per request.


📝 Documentation: ✅ LGTM

The comment on the new flag and the code comments match the behaviour.


Reviewed commit: d8c97881 — same changes as 8c241596, so this is that commit's review (no new review was run; line numbers are as of 8c241596)

@ryanking13
ryanking13 merged commit 5931977 into main Nov 13, 2026
24 checks passed
@ryanking13
ryanking13 deleted the gyeongjae/keep-zip-directory-cache branch November 13, 2026 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants