Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request implements incremental expansion (requirement 15) for the Craft engine in Dart, allowing the runtime to reuse unchanged subtrees from previous expansions. It tracks epochs for data, media, and state changes to optimize reuse checks, and updates CraftView in a2ui_craft_flutter to avoid rebuilding subtrees of reused nodes. Additionally, it adds comprehensive equivalence tests and performance benchmarks. Feedback suggests generalizing the parameter types in _sameKeys within reuse.dart to support maps with more general key types.
| bool _sameKeys(Map<String, Object?> a, Map<String, Object?> b) { | ||
| if (a.length != b.length) return false; | ||
| final Iterator<String> other = b.keys.iterator; | ||
| for (final String key in a.keys) { | ||
| if (!other.moveNext() || other.current != key) return false; | ||
| } | ||
| return true; | ||
| } |
There was a problem hiding this comment.
Using Map<String, Object?> for _sameKeys restricts key comparison to maps statically typed with String keys. To make the key comparison more robust and support maps with more general types (such as Map<Object?, Object?> or Map<dynamic, dynamic>) that still contain string keys, consider generalizing the parameter types to Map<Object?, Object?>.
| bool _sameKeys(Map<String, Object?> a, Map<String, Object?> b) { | |
| if (a.length != b.length) return false; | |
| final Iterator<String> other = b.keys.iterator; | |
| for (final String key in a.keys) { | |
| if (!other.moveNext() || other.current != key) return false; | |
| } | |
| return true; | |
| } | |
| bool _sameKeys(Map<Object?, Object?> a, Map<Object?, Object?> b) { | |
| if (a.length != b.length) return false; | |
| final Iterator<Object?> other = b.keys.iterator; | |
| for (final Object? key in a.keys) { | |
| if (!other.moveNext() || other.current != key) return false; | |
| } | |
| return true; | |
| } |
dda3123 to
7ca077a
Compare
Expanding the root again reuses the subtree of each template instance (and of the root) whose inputs did not change, so an update costs about what it changed instead of the whole tree; the result is always what expanding every node gives. - craft_runtime requirement 15: every expansion equals a full expansion (canonical form, errors with templates and positions, states, firing, builder outputs, steps and nodes); limits apply to the whole tree; the engine tells adapters which nodes are unchanged; what a template instance depends on. craft_limits requirement 8: budgets count reused parts; an expansion reports its steps and nodes. - lib/src/reuse.dart (part of runtime.dart): frames record the arguments of the call that they read (values, order, depth), observations of data, state, media and registry answers (with input epochs for cheap checks), instances, errors and counts; reuse reads the arguments again so each is still evaluated once at the depth of its first read; a pass stopped by a limit is redone without reuse. Expansion.steps/nodes, CraftRuntime.incremental, TemplateRegistry.revision. The classification of each call node is remembered until the registrations change. - CraftView keeps one widget per node, so Flutter skips reused subtrees. - Tests: 8 conformance cases (F-CRAFT-RT-15); an equivalence test running random updates on every runtime/media/limits case and on random templates under random limits, comparing with a runtime that does not reuse (also under dart test -p node); unit tests for what is reused and the corner cases; widget tests counting primitive builds. - Benchmarks (dart test -P performance, new CI job craft-performance): growth n vs 4n and reuse vs full for a list with one row changing, a form with one keystroke, a deep template chain, and every row changing.
7ca077a to
8437999
Compare
Description
Stacked on #58 (craft/stdlib).
What
Expanding the root again reuses the subtree of every template instance (and of the root) whose inputs did not change. The result is always exactly what expanding every node gives; only the cost changes. CraftView renders a reused node as the identical widget, so Flutter skips those subtrees.
Spec
craft_runtimereq 15 (new): every expansion equals a full expansion (canonical form, error templates/positions/messages, states, firing incl. fire-time payloads, builder outputs, steps and nodes); limits apply to the whole tree (reused parts count as re-evaluated, stops exactly where/with the kind a full expansion would); adapters can tell unchanged nodes; SHOULD-level description of what a template instance depends on.craft_limitsreq 8: budgets count reused parts; an expansion MUST report the steps and nodes it counted.Design
CraftRuntime(incremental: true)(false = full expansion, for comparison),Expansion.steps/nodes,TemplateRegistry.revision. Adapters: a node identical to one of the previous expansion is unchanged with its subtree.Tests
dart testanddart test -p node(~4 s VM);CRAFT_EQUIVALENCE_ROUNDSscales it (30 rounds: 4,633 tests pass).Benchmarks (VM, ms per update, one root expansion per update as a view does; median of 15×10 updates; before = #58 head 340b93b)
Lazy lists gain nothing in the engine (a template holding a builder re-expands, and visible outputs are expanded again);
...forlists and template instances are what gets faster.Flutter (widget tests, primitive builds per update): 50-row list, one row changes: 151 → 4; form, one keystroke: all fields → the field typed into (4 builds); on an A2UI surface, a keystroke in a field bound to the data model rebuilds only that field (renderer test).
CI (
craft-performancejob,dart test -P performance): growth 1000 vs 250 rows ≤ 8, 180 vs 45 fields ≤ 8, lazy list 4000 vs 1000 rows ≤ 8, depth 300 vs 75 ≤ 32 on the fastest runs (node paths grow with depth, so n² — up to 16, 9–13 measured idle or loaded; per-level bookkeeping in depth would be n³, up to 64; 32 is halfway on a log scale); reuse vs full ≤ 0.8 (list), ≤ 0.7 (form), ≤ 0.8 (chain), ≤ 1.5 (lazy list), ≤ 3 when every row changes.Review fixes
Known limits
An independent review compared the reusing and non-reusing runtimes with its own differential harness: about 26,000 random programs and 815,000 expansions (128,000 stopped at a limit), with no mismatch.
Pre-launch Checklist
python3 ./scripts/fix_licenses.py --check)../scripts/fix_format.sh --check).