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 — zipimport reads and unmarshals pyc's twice per import · Issue #161644 · python/cpython

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

New issue
Copy link
New issue
Copy link
Open
Open
zipimport reads and unmarshals pyc's twice per import#161644
Copy link
Assignees
itamaro
Labels
performancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-importlibtype-featureA feature request or enhancementA feature request or enhancement

Description

@itamaro
itamaro
opened on Nov 8, 2026
Issue body actions

Feature or enhancement

Importing a module from a .pyc in a zip runs marshal.loads() on it twice: once in find_spec() (get_filename() → _get_module_code()) and throws the code object away, and again in exec_module().

get_filename() only needs the 16-byte header to decide whether to use the pyc or not. a bad header (magic, flags, stale timestamp/size, hash mismatch) makes zipimport fall back to the sibling .py. errors in the marshalled body don't, they just fail the import. we can validate the header and stop there.

while making this change would be user-visible (e.g., a pyc with a good header and a corrupt body will fail in exec_module() rather than find_spec()), this is similar to gh-153799 (issue gh-141276), so should be acceptable (especially if we don't backport this one).

filing as a separate issue, as followup to a past comment on gh-141276.

testing this with a draft patch on a release build, avoiding the throw-away unmarshal yields up to 50% speedup in imports from pycs in zips, while making find_spec constant time instead of O(pyc size).

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

  • gh-161644: Avoid unmarshalling zipped .pyc files twice on import #161700

Activity

  1. itamaro
    self-assigned this
    on Nov 8, 2026
  2. itamaro
    added
    type-featureA feature request or enhancementA feature request or enhancement
    performancePerformance or resource usagePerformance or resource usage
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    topic-importlib
    on Nov 8, 2026
  3. itamaro commented on Nov 8, 2026

    @itamaro
    itamaro
    on Nov 8, 2026
    ContributorAuthor
    More actions

    I'll open a PR with more benchmarking details based on the draft patch I mentioned, if there are no objections

  4. itamaro
    added 7 commits that reference this issue on Nov 8, 2026

    pythongh-161644: Only validate the pyc header in zipimporter.get_file…

    e5b8bcc

    pythongh-161644: Only validate the pyc header in zipimporter.get_file…

    a8401ea

    pythongh-161644: Only validate the pyc header in zipimporter.get_file…

    17d1710

    pythongh-161644: Only validate the pyc header in zipimporter.get_file…

    e7052f0

    pythongh-161644: Avoid unmarshalling zipped .pyc files twice on import

    11a28d4

    pythongh-161644: Avoid unmarshalling zipped .pyc files twice on import

    aa74dcc

    pythongh-161644: Avoid unmarshalling zipped .pyc files twice on import

    4ecb6d1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @itamaro
    itamaro

Labels

performancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-importlibtype-featureA feature request or enhancementA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    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.