Repository navigation
Description
Problem
check_model_units (src/simlin-engine/src/db/units.rs, the #[salsa::tracked] fn at line 165) is a PER-MODEL tracked query, but its body reconstructs the whole project to unit-check one target model:
- The
build_model_s0closure (~lines 190-232) rebuilds aModelStage0for EVERY model inproject.models(db), cloning every parsed variable out of the salsa cache (parsed.variable.clone()at line 198) and re-parsing implicit SMOOTH/DELAY expansion vars. - All of those
ModelStage0s are then lowered toModelStage1viaModelStage1::new(~lines 238-248). - Only after that does the function pick out the single target model (line 254) and run inference/checking on it.
Parsing itself is salsa-cached, but the Stage0 map construction and the Stage1 lowering are plain code inside the tracked body, so they run fresh on every invocation. Collecting diagnostics for all M models of a project (collect_all_diagnostics -> model_all_diagnostics -> check_model_units per model) therefore costs M x (full-project Stage0+Stage1 reconstruction) -- quadratic in model count.
The cross-module rebuild is not gratuitous in itself -- unit inference needs the full models_s1 map so module input/output constraints can resolve submodel variable types -- but building it per target model instead of once per project is.
Why it matters
- Macro-rich projects are hit hardest: each
:MACRO:is a project model (C-LEARN has dozens). Macro-marked models are skipped as check targets (line 181), but they are still rebuilt into the Stage0/Stage1 maps on every invocation, so they multiply the per-invocation cost without ever being checked themselves. - Unit checking runs on every diagnostics collection (editor error panels, MCP
read_model/edit_model,simlin_project_get_errors), so this cost is on the interactive path, not just batch compiles. - It also defeats salsa incrementality within the query: any variable edit in any model invalidates every model's
check_model_units(they all read every model's parse results), and each re-run pays the full reconstruction again.
Components affected
src/simlin-engine/src/db/units.rs(check_model_units)- Indirectly:
src/simlin-engine/src/db/diagnostic.rs(model_all_diagnostics,collect_all_diagnostics) as the fan-out drivers
Possible approaches
- Near-term: introduce a project-level salsa-tracked query that builds the Stage0 map (and, if
ModelStage1can be madesalsa::Update-compatible or wrapped inArc, the Stage1 map) once per project revision, shared by all per-model unit checks. This drops the cost from M x project to 1 x project per revision. - Longer-term: a canonical salsa-tracked lowered-variable representation that units checking consumes directly instead of re-lowering through the legacy
ModelStage0/ModelStage1shapes. This is the same "reconstruction boundary" direction discussed in engine: emit layout-independent fragments directly #964 (fragment compiler fake-Module bridge) and adjacent to ltm: C-LEARN LTM compile-time dominated by per-fragment fixed overhead (DimensionsContext clone, O(D^2) intern_name, repeated parse) #655 (LTM per-fragment fixed overhead); neither of those issues covers the units path.
Related (not duplicates)
- engine: emit layout-independent fragments directly #964 -- fragment compiler builds a fake
ModelStage0/mini-layout per variable; same pattern, different pipeline. - ltm: C-LEARN LTM compile-time dominated by per-fragment fixed overhead (DimensionsContext clone, O(D^2) intern_name, repeated parse) #655 -- LTM per-fragment fixed overhead; same "small constant times huge N" shape, LTM-specific.
- engine: unit inference deep-copies unit maps on every clone #318 --
UnitMapclone cost inside inference; orthogonal micro-cost within a single check. - docs/tech-debt.md item on legacy
errors/unit_errorsfields notesModelStage0/ModelStage1are legacy-shaped carriers, but tracks the dead error fields, not this rebuild cost.
Context
Identified during a simlin-engine architecture review (2026-07-14) while examining the reconstruction-boundary pattern around #964.
Activity