Skip to content

build_dist.py Re-Resolves the Lazily Imported Audit Module on Every Line It Reads #1424

Closed

Description

@ptr727

scripts/build_dist.py's _fence_step scans sys.path and re-enters import audit on every call, and every caller calls it once per line of the document being read. scripts/canonical_review.py reaches audit._fence_step the same way, through a module it imports at the top, so that one pays the sys.modules lookup rather than the sys.path scan.

Measurement

The same shape in scripts/carry.py, fixed on PR #1423 by putting functools.cache on the accessor, measured on this repository's own GOVERNANCE.md, 333 lines and 22 level-two sections:

one pass locating every section
accessor re-resolved per line 28.8 ms, over 7326 calls
accessor cached 0.9 ms

build_dist.py reads every authored skill on each --check and each regeneration, and --check runs in CI on every pull request, so the cost is paid there rather than only in an interactive run. The exact figure for this file is unmeasured, and the shape is identical.

What it is not

The laziness itself is deliberate and stays. scripts/skills_install.py imports build_dist on hosts whose Python predates what audit.py needs, and installing never fills an include region, so importing audit at module scope would break that path. What is wrong is that "on first use" currently means "on every use". functools.cache on the accessor keeps the first property and fixes the second.

Suggested shape

Split the accessor out the way carry.py now has it, a cached zero-argument audit_module() that _fence_step delegates to, rather than caching _fence_step itself, whose arguments vary per line and would make a cache unbounded.

Filed from #1423's review rather than widened into it, since build_dist.py is not among that pull request's changed files.

Activity

  1. added a commit that references this issue on Sep 7, 2026
    5ae7cf8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions