GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-161644: Avoid unmarshalling zipped .pyc files twice on import by itamaro · Pull Request #161700 · python/cpython

Conversation

@itamaro

@itamaro itamaro commented Nov 9, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

zipimporter.get_filename() (and therefore find_spec()) read, decompressed and unmarshalled the whole .pyc just to determine the module's file name, and exec_module() then did it all again. Only the 16-byte header matters for picking the file: a bad or stale header falls back to the sibling .py, while errors in the marshalled body never do.

This splits the header checks out of _unmarshal_code() and has get_filename() run only those, reading (and for deflate/zstd members, decompressing) just enough of the member to cover the header.

Observable change: a .pyc with a valid header and a corrupt body now fails in exec_module() instead of find_spec(), with the same exception. GH-153799 made the equivalent change for a SyntaxError in source modules.

Benchmarks

Release build, Linux, warm page cache, timestamp-based pycs.

Importing 27 stdlib packages (148 modules) from a zip of ~350 stdlib modules with .pyc next to .py, min of 40 runs:

compression main this PR
stored 96 ms 79 ms −18%
deflate 131 ms 99 ms −25%
zstd 141 ms 125 ms −12%

Per module, find_spec() + get_code() for a single .pyc (in-process medians):

pyc size stored deflate zstd
10 KB −6% −1% −10%
50 KB −32% −30% −34%
500 KB −49% −46% −44%
5 MB −44% −41% −34%

find_spec() itself no longer scales with pyc size: ~0.1 ms regardless of size, versus 43 ms for a 5 MB pyc on main.

zstd gains less because ZstdDecompressor construction dominates small reads and zstd decodes a whole block before producing output.

  • Issue: zipimport reads and unmarshals pyc's twice per import #161644

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@itamaro @claude
pythongh-161644: Avoid unmarshalling zipped .pyc files twice on import
4ecb6d1
`get_filename()` (and so `find_spec()`) read, decompressed and
unmarshalled the whole `.pyc` just to learn the file name, and
`exec_module()` then did it all again.  Only the 16-byte header matters
for picking the file: a bad or stale header falls back to the sibling
`.py`, while errors in the marshalled body never do.

Split the header checks out of `_unmarshal_code()` and have
`get_filename()` run only those, reading (and decompressing) just
enough of the member to cover the header.  A `.pyc` with a valid header
and a corrupt body now fails in `exec_module()` instead of
`find_spec()`, with the same exception.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@itamaro itamaro added performance Performance or resource usage stdlib Standard Library Python modules in the Lib/ directory topic-importlib labels Nov 9, 2026
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 9, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Nov 9, 2026
zipimport reads and unmarshals pyc's twice per import #161644
Open
@read-the-docs-community

read-the-docs-community Bot commented Nov 9, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35765280 | 📁 Comparing 4ecb6d1 against main (e3262d9)

  🔍 Preview build  

2 files changed
± library/zipimport.html
± whatsnew/changelog.html
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@itamaro
itamaro marked this pull request as ready for review November 9, 2026 20:57
@itamaro
itamaro requested review from FFY00, brettcannon, ericsnowcurrently, ncoghlan and warsaw as code owners November 9, 2026 20:57
@bedevere-app bedevere-app Bot added the awaiting core review label Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@brettcannon brettcannon Awaiting requested review from brettcannon brettcannon is a code owner
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner
@ncoghlan ncoghlan Awaiting requested review from ncoghlan ncoghlan is a code owner
@warsaw warsaw Awaiting requested review from warsaw warsaw is a code owner
@FFY00 FFY00 Awaiting requested review from FFY00 FFY00 is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting core review performance Performance or resource usage stdlib Standard Library Python modules in the Lib/ directory topic-importlib type-feature A feature request or enhancement

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@itamaro
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.