Skip to content

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

Open

Description

@itamaro

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

Activity

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

    @itamaro
    ContributorAuthor

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

  4. added 7 commits that reference this issue on Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

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

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions