gh-161644: Avoid unmarshalling zipped .pyc files twice on import #161700
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
brettcannon
zipimporter.get_filename()(and thereforefind_spec()) read, decompressed and unmarshalled the whole.pycjust to determine the module's file name, andexec_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 hasget_filename()run only those, reading (and for deflate/zstd members, decompressing) just enough of the member to cover the header.Observable change: a
.pycwith a valid header and a corrupt body now fails inexec_module()instead offind_spec(), with the same exception. GH-153799 made the equivalent change for aSyntaxErrorin 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
.pycnext 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 onmain.zstd gains less because
ZstdDecompressorconstruction dominates small reads and zstd decodes a whole block before producing output.<!-- gh-issue-number: gh-161644 -->
<!-- /gh-issue-number -->