Skip to content

perf(install): skip the receipt lookup when planning an identical file - #4771

Merged
Daniel Meppiel (danielmeppiel) merged 4 commits into
mainfrom
danielmeppiel-perf-uv-comparison
Nov 22, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 4 commits into
mainfrom
danielmeppiel-perf-uv-comparison

Conversation

@danielmeppiel

@danielmeppiel Daniel Meppiel (danielmeppiel) commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Description

perf(install): L2 first slice of rt-06 (the no-op integrate path).

check_before_deploy runs the #4381 overwrite preflight: _planned_keeps re-runs every gated integrator (prompts, instructions, agents, commands) in planning mode, before the real run. For each file whose on-disk bytes already match the render, _check_adopt_or_skip then called receipted_unchanged. That call locates the receipt twice (_write_locator), checks the lock claim and re-hashes the file. Planning records only the files it would keep, and an identical file is never kept. Its receipt therefore cannot change the plan, and the lookup was pure cost.

The change: while planning (_overwrite_plan is not None), an identical file returns (skip=True, adopted=False) without the receipt lookup. The real run is unchanged and still reads every receipt.

Gains (GAINS STANDARD). Journey: apm install no-op, run a second time on an installed project. Fixture: tests/fixtures/perf tree-10 (seed 10), offline mirror, rtt 0. Base main 84a893b -> head 8a4f0cb (src change; later commits touch tests only). Interpreter: CPython 3.12, the same venv for both sides, sides alternated by PYTHONPATH. Every no-op left the project bytes identical, with 41 mirror requests on both sides.

run (ABAB) side CPU user+sys incl. children (s) wall (s) load1
0 main 3.503 29.7 48.9
0 head 4.921 106.1 52.2
1 main 3.393 21.3 47.4
1 head 2.895 15.5 42.8
2 main 3.014 19.0 45.4
2 head 3.203 25.8 42.7
p50 main 3.393 / head 3.203 -0.19 s CPU (1.06x, -5.6% of the whole no-op) wall not valid (load > 30)

CPU p50 is noisy at n=3 under load 43-52; the deterministic evidence is the call counts (cProfile on the process_time clock, one run per side):

counter (tree-10 no-op) main head delta
receipted_unchanged 184 94 -49%
_trusted_hash 224 134 -40%
locator_for_path 1130 860 -24%
compute_file_hash 576 486 -16%
_check_adopt_or_skip cum CPU (profiled) 1.439 s 0.869 s -40%
check_before_deploy cum CPU (profiled) 1.996 s 1.634 s -18%

Against the 1 s target: tree-10 no-op is at ~3.2 s CPU p50 after this slice (main 3.4 s). This slice is about 6% of the gap. The gap is dominated by integrate (skill integration, security scan, skill preflight, locators: L1) and startup/imports (L3).
tree-50: the counts scale linearly (see below), so the projected cut is ~450 receipted_unchanged calls of 917. The ABAB is PENDING (PERF host slot queue).

Findings (main 84a893b, deterministic counts):

  • _planned_keeps is O(N), not O(N^2). From tree-10 to tree-50, every count scales by exactly 5x: _planned_keeps 10->50, _check_adopt_or_skip 180->900, locator_for_path 1130->5635, compute_file_hash 576->2868. The cost is one duplicate render pass of the gated primitives. Its profiled share also carries the run's first-touch costs (the discover_primitives cache misses land in planning, not in the real pass).
  • Already done on main, so not in this PR: discover_primitives is memoized per root (80 calls, 10 misses = 1 per package), and the lockfile YAML parse is memoized (perf(lockfile): parse each lockfile text once and load it in O(N) #4735: 1 parse, 19 deep copies).

Security invariants (H8):

Issue and approved scope

Issue: Refs #4732

Human scope-approval comment: PERF charter on #4732 (CEO re-charter 16:08: tree-10 no-op <= 1 s; rt-06 lane L2, integrate plan/hash).

Does this PR complete the issue, or what remains? It is the first L2 slice. Still to come: reusing planning-pass work in the real run, deduping per-run content hashes, and passing the loaded lockfile down instead of 19 deep copies.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally
  • All existing tests pass (CI; main is red on exec_gate.py LOC 1007/1000, which is not this PR)
  • Added tests for new functionality (if applicable)

tests/unit/integration/test_content_identical_adopt.py::TestOverwritePlanSkipsReceiptOnIdentical: planning an identical file reads no receipt and adds nothing to the plan (red before this change: the receipt is read). The real run still reads it.

Changelog

  • One fragment per section, changelog.d/<pr>.<section>.md, holding
    one line: user-visible effect first, 25 words or fewer, ending with
    (#PR); breaking changes only under upgrade notes; no Highlights; no
    new lines under ## [Unreleased]. See changelog.d/README.md and
    .github/instructions/changelog.instructions.md.
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

  • Spec edit: docs/src/content/docs/specs/openapm-v0.1.md updated
    (new/changed <a id="req-XXX"></a> anchor + prose + Appendix C
    row).
  • Manifest edit: docs/src/content/docs/specs/manifests/openapm-v0.1.requirements.yml
    updated.
  • Test edit: a @pytest.mark.req("req-XXX") test under
    tests/spec_conformance/ added or extended.
  • CONFORMANCE.{md,json} regenerated via
    uv run --extra dev python -m tests.spec_conformance.gen_statement
    and committed.
  • N/A -- this PR does not change OpenAPM-observable behaviour.

The #4381 overwrite preflight re-runs every gated integrator in planning
mode. Planning records only the files it would keep, and a file whose
bytes already match the render is never kept, so its deployment receipt
cannot change the plan. Skip receipted_unchanged (receipt locate, claim
check and content hash) for identical files while planning; the real
run still reads every receipt.

Refs #4732

Co-authored-by: Copilot App <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Both new tests fail on Windows because Path.write_text converts LF to CRLF, preventing the fixtures from exercising the identical-file branch; they need explicit UTF-8 bytes.

1 open issue
What changed in this PR

This PR reduces no-op install work by skipping receipt lookups for byte-identical files during overwrite planning, while preserving receipt checks during actual deployment.

Changes:

  • Return early for identical files in planning mode without marking them as adopted.
  • Add tests for planning-time receipt skipping and continued receipt checks during the real run.
File Description
tests/​unit/​integration/​test_content_identical_adopt.py Adds planning and real-run receipt lookup tests.
src/​apm_cli/​integration/​base_integrator.py Skips unnecessary receipt checks for identical files during planning.

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The cited tests-only approval explicitly excludes source changes and optimizations, leaving this product optimization without a linked, applicable scope approval.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Low severity Optimization lacks a matching approval scope record

src/​apm_cli/​integration/​base_integrator.py:854

The approval record on #4732 (#4732 (comment)) is tests-only and explicitly excludes changes under src/ and optimizations, requiring separate scope records. This product optimization therefore is not covered by that record, contrary to the PR description's scope reference. Please link the newer human approval comment for the claimed rt-06/L2 re-charter, or obtain a bounded optimization scope record and update the PR description.

1 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human confirmation of the optimization scope is needed because the fetched #4732 comments approve tests only and exclude source changes and optimizations.

No open issues 🎉

Review effort: Balanced

@danielmeppiel
Daniel Meppiel (danielmeppiel) marked this pull request as ready for review November 22, 2026 17:06
@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit ce977b2 into main Nov 22, 2026
69 of 74 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-perf-uv-comparison branch November 22, 2026 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants