Skip to content

perf(diff): take the snapshots from the Unit table and read only the units whose hash changed - #2022

Draft
italo-kalil-cesar wants to merge 4 commits into
mendixlabs:mainfrom
italo-kalil-cesar:pr/diff-table-snapshot
Draft

italo-kalil-cesar wants to merge 4 commits into
mendixlabs:mainfrom
italo-kalil-cesar:pr/diff-table-snapshot

Conversation

@italo-kalil-cesar

Copy link
Copy Markdown

Fixes #2014

Opened as a draft, stacked on the diff-parallel PR (#2021, branch pr/diff-parallel): its two commits come first in this branch. Review only the last two. The gain here is small (see below), so the issue asks whether you want it at all.

What was wrong

mxcli diff took two snapshots of the scratch copy, each reading every unit file (3.1-5.8 s and 1.2-2.0 s on a project of about 3 thousand units, 1 GB).

What changes

  1. mpr: UnitStamps gives each unit's name and ContentsHash without opening files: Reader.UnitStamps returns, for an MPR v2 project, each unit's name and the Unit table's ContentsHash (SHA-256 of its file) from the table and the type index. MPR v1 returns nil and the caller reads what it needs.
  2. scriptdiff: snapshot from the Unit table; read only the units whose hash changed: Run takes both snapshots from the table; Compare reads a unit's bytes only when the two hashes differ, the unit moved, or it was added or removed. The bytes from before the script come from the project the copy was made from, since the copy is rewritten by then. Units the reader cannot stamp (MPR v1, packages) are read as before; a row without a hash is compared by its bytes; a unit whose bytes cannot be read is reported as rewritten, never dropped. The public TakeSnapshot is unchanged. CHANGELOG has an entry.

Honest size of the gain

Same project, MXCLI_DIFF_TIMING, 3 runs each, alternating: snapshot-before 3.1-5.8 s -> 0.6 s; snapshot-after 1.2-2.0 s -> 0.5-0.7 s. But exec inside the diff got about 1.2 s slower (1.5-3.3 s -> 2.7-3.0 s): the old eager snapshot warmed the copy's cache, which exec then used. Total 15.3 / 15.9 / 22.5 s -> 13.8 / 13.2 / 12.4 s, so about 2 to 3 s net out of about 15 s. The copy (about 6.5 s) and the cleanup (about 1.6 s) are unchanged and are now most of the time.

Tests

snapshot_table_test.go on the PedApp fixture: the table snapshot has the same units as the eager one and reads no file; Compare over an added, removed, rewritten and moved unit returns the same changes, ids, kinds and bytes as the eager Compare while reading only the changed units; with every hash blanked it still matches. reader_unit_stamps_test.go: every stamp equals the file it describes on a warm index with no unit file opened; MPR v1 gives none. The existing diff-versus-exec tests run through the new Run. The built binary gives the same diff --format json report as the previous branch on the same script.

Run locally

Run on Windows without make; the equivalents were run by hand:

  • go build ./...: clean
  • go vet on the touched packages: clean
  • gofmt -l on the touched files: empty
  • go test -timeout ... on the touched packages: see Tests
  • make check-test-timeouts, make check-findings: not run (bash and make are not available here); no Makefile or findings file is touched

Studio Pro validation

Not run. diff writes nothing to the project.

Agentic testing

Not run.

italo-kalil-cesar and others added 4 commits December 1, 2026 22:53
mxcli diff spent most of its time (about 18 s of 26 s on a 3,100-unit project,
Windows) copying the project one file at a time, then hashing the files next to
the model and removing the scratch folder one file at a time.

- copyProject: regular files are copied by a pool of workers (directories and
  symlinks are still created in walk order).
- TakeSnapshot: the files next to the model are hashed by a pool of workers.
- Scratch.Close: the first three levels of the scratch folder are removed in
  parallel.
- packages/ (deployment packages exported by Studio Pro) joins the skipped
  directories: build output that exec never reads or writes.
- MXCLI_DIFF_TIMING=1 prints the time of each stage of a diff to stderr.

Measured on one project (3,138 units, ~430 MB outside the skipped directories):
diff of a one-statement script 30.8 s -> about 13 s; same output as before on
the scripts tried.

Tests: the copy skips the build directories and copies every file, the parallel
removal removes the tree, the file hash is SHA-256.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
For a tool that has to decide on the result of a diff without parsing the text:
refused, preflight, execError, failures, units, documents, files and summary.
A script exec would refuse reports refused and summary.writesNothing.

Docs: the diff page gains a JSON output section, the flag help and the --json
refusal hint name the format, and CHANGELOG has the entry.

Tests: a modified document and file are reported with the right kinds and
summary; a refused script has writesNothing and empty (not null) lists.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ng files

For a caller that only needs to know which units changed between two moments:
the Unit table's ContentsHash (the SHA-256 of the unit's file) and the name the
type index holds. MPR v1 has none (nil), and the caller reads what it needs.
The test checks every stamp against the file it describes, on a warm index
with no unit file opened.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ash changed

diff took two snapshots that each read every unit file of the scratch copy.
Run now takes them from the table (type, name, place and ContentsHash, via
mpr.UnitStamps) and Compare reads a unit's bytes only when the two hashes
differ, the unit moved, or it was added or removed. What a unit was before is
read from the project the copy was made from, since the copy is rewritten by
then. Units the reader cannot stamp (MPR v1, packages) are read as before, a
row without a hash is compared by its bytes, and a unit whose bytes cannot be
read is reported as rewritten rather than dropped. TakeSnapshot is unchanged.

Tests (PedApp fixture): the table snapshot has the same units as the eager one
and reads no file; Compare over an added, removed, rewritten and moved unit
returns the same changes, ids, kinds and bytes as the eager Compare while
reading only the changed units; with every hash blanked it still matches. The
existing diff-versus-exec tests run through the new Run.

Measured on a 3137-unit, 1 GB project (MXCLI_DIFF_TIMING, 3 runs each,
alternating): snapshot-before 3.1-5.8 s -> 0.6 s; snapshot-after 1.2-2.0 s ->
0.5-0.7 s; exec 1.5-3.3 s -> 2.7-3.0 s (the old eager snapshot warmed the
copy's cache); total 15.3 / 15.9 / 22.5 s -> 13.8 / 13.2 / 12.4 s. The copy
(6.3-6.9 s) and cleanup (1.5-2.1 s) are untouched.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

perf(diff): take the before/after snapshots from the Unit table and read only the units whose ContentsHash changed

1 participant