Skip to content

engine: check_model_units rebuilds Stage0/Stage1 for the whole project per target model (quadratic in model count) #966

Closed

Description

@bpowers

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_s0 closure (~lines 190-232) rebuilds a ModelStage0 for EVERY model in project.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 to ModelStage1 via ModelStage1::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

Related (not duplicates)

Context

Identified during a simlin-engine architecture review (2026-07-14) while examining the reconstruction-boundary pattern around #964.

Activity

  1. added
    engine
    rust
    on Jul 15, 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

    enginerusttech debt

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions