Skip to content

perf: lockfile is re-parsed ~19x per install with an O(N^2) ledger rebuild #4733

Description

Summary

Every apm install (cold, warm, no-op and --frozen) and apm update reads apm.lock.yaml about 19 times, from 14 call sites. Each read re-parses the whole file with the pure-Python bounded SafeLoader. Loading then rebuilds the legacy deployment-ledger projection once per added dependency (DeploymentLedgerCodec.invalidate_legacy_projection), which is O(N^2), and the result is discarded at the end of _from_validated_data.

Evidence at main 4aabb87 (offline mirror, perf fixtures, #4732): a no-op install of tree-50 takes 66 s, of which LockFile.read x19 is 35.4 s (54%), with 745k _legacy_locator calls. For tree-10, the reads are 4.3 s of 9.9 s.

Scope

  • Memoize only the pure YAML parse, keyed by the exact lockfile text (bounded LRU, deep copy out). Container validation and object construction still run on every read.
  • Skip the per-row legacy-ledger rebuild during load only when the result is provably identical, and keep the per-row path otherwise. An equivalence test covers this.

Done when

The lockfile cost of a no-op/warm/--frozen install no longer grows quadratically with N, there are before/after numbers at exact SHAs for tree-10/50 (and 200), behavior is unchanged (targeted tests, lint mirror, lifecycle_smoke), and QL has signed off under H8.

Out of scope

Any change to integrity, hash, trust or policy checks. Lockfile schema changes.

Review contact

Daniel Meppiel (@danielmeppiel) (PERF function head, on behalf of the CEO)

Activity

  1. added
    performance
    on Nov 22, 2026
  2. danielmeppiel commented on Nov 22, 2026

    @danielmeppiel
    CollaboratorAuthor

    Decision: approve
    Area: project
    Scope: Performance only. LockFile.read memoizes the pure YAML parse of apm.lock.yaml, keyed by the exact file text, in a bounded LRU that returns a deep copy; container validation and object construction still run on every read, so a changed file is always re-parsed and re-validated. Loading skips the per-row legacy deployment-ledger rebuild only where the result is provably identical, and keeps the per-row path otherwise. No integrity, hash, trust or policy check changes, and the lockfile schema is unchanged.
    Done when: An equivalence test shows the fast load path yields objects identical to the per-row path across the perf fixtures; a test shows an edited lockfile is re-parsed and re-validated (no stale cache hit); before/after numbers at exact SHAs for tree-10 and tree-50 (and tree-200) show the lockfile cost of a no-op, warm and --frozen install no longer grows quadratically with N; targeted tests, the lint mirror and lifecycle_smoke are green; and QL signs off under H8 before landing. A CHANGELOG line is added.
    Out of scope: Any change to integrity, hash, trust or policy checks; lockfile schema changes; other performance work.
    Review contact: Daniel Meppiel (@danielmeppiel)

  3. added
    status/accepted
    on Nov 22, 2026
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

    performancestatus/accepted

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions