← Back to Pull requests

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

Open
unknown opened this pull request with 216 lines diff across 2 files
View on GitHub
unknown unknown commented on Nov 09, 2026 19:43 UTC
Author

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.

<!-- gh-issue-number: gh-161644 -->

  • Issue: gh-161644

  • <!-- /gh-issue-number -->

    Diff: 216 lines (+162 / -54) across 2 files Reviewer: brettcannon Review duration: 36221m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.37) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.37 · 216 lines diff across 2 files
    0.37
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Shallow Review Depth shallow_review_depth
    0.344
    Diff Size diff_size
    0.322
    Real PR Conversation Timeline
    1 comments
    "Review active on #161700"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 36.6% (MEDIUM) · Higher = larger blast radius
    36.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.5% · Safe to merge
    19.5%
    Repository