Repository navigation
Description
Problem
ModelStage0 and ModelStage1 (src/simlin-engine/src/model.rs:51-78) and db::query::ParsedVariableResult (src/simlin-engine/src/db/query.rs:26) all take a derived PartialEq alongside salsa::Update:
#[cfg_attr(feature = "debug-derive", derive(Debug))]
#[derive(Clone, PartialEq, salsa::Update)]
pub struct ModelStage0 { /* variables: HashMap<Ident<Canonical>, VariableStage0>, ... */ }Their variables carry parsed ASTs, and Expr0::Const(String, f64, Loc) / Expr2::Const hold a bare f64 compared with ==. Since NaN != NaN, a stage value holding a NaN literal can never compare equal to itself.
Salsa decides whether to backdate a re-executed tracked function's memo by comparing old and new with PartialEq (salsa::Update's derive compares fields independently, so it inherits the same defect). A memo that never compares equal to itself is never backdated: every re-execution reports "changed", and every dependent query re-executes, even when the recomputed value is bit-identical.
Reachability: every project, no user action required
This is the part that differs from the two existing issues below. nan is a lexer keyword (("nan", Nan), src/simlin-engine/src/lexer/mod.rs:86), and every stdlib SMOOTH/DELAY/TREND template declares initial_value = NAN:
stdlib/smth1.stmx:29: <eqn>NAN</eqn>
stdlib/smth3.stmx:43: <eqn>NAN</eqn>
stdlib/delay1.stmx:30: <eqn>NAN</eqn>
stdlib/delay3.stmx:30: <eqn>NAN</eqn>
stdlib/trend.stmx:32: <eqn>NAN</eqn>
db::sync splices those templates into every project, so every project already carries several stage values (and several ParsedVariableResult memos) that are unequal to themselves. No user-authored NaN is needed.
Why it is harmless today, and what makes it stop being harmless
Today the affected values sit on stdlib models, whose salsa inputs never change after sync. A query whose inputs' revisions did not move is a cache hit decided by input revisions, not by the output's PartialEq, so the non-reflexive memo is never re-executed and never gets the chance to fail to backdate.
That is a property of which models happen to contain NaN, not of the types. The moment a user model containing a NaN literal sits upstream of a cached stage -- or a stdlib input is touched -- the whole downstream cone loses backdating: every edit anywhere in that model re-runs model_stage0 -> model_stage1 -> check_model_units (and, for ParsedVariableResult, every consumer of the parse) with no reuse, because each stage reports "changed" on a bit-identical rebuild. That is an incremental-recompilation cliff with no compile error and no test to catch it: the newly-cached stage queries (GH #966) put both stage types on the interactive diagnostics path, where the cost is paid per keystroke.
Practical consequence observed in testing
While writing db/stages_tests.rs for #966, the stdlib-model value oracle had to be written against npv rather than the natural choice smth1: a smth1 stage is not equal to an independently-rebuilt, bit-identical smth1 stage, so assert_eq! fails. The test carries a comment saying so. A test that cannot assert equality on a value it just rebuilt identically is the observable symptom of the defect.
Components affected
src/simlin-engine/src/model.rs--ModelStage0,ModelStage1(derivedPartialEq+salsa::Update)src/simlin-engine/src/db/query.rs--ParsedVariableResult(same derives; thereturns(ref)memo ofparse_source_variable_with_module_context, which is upstream of both stages)src/simlin-engine/src/db/stages.rs--model_stage0/model_stage1, the queries whose backdating is defeatedsrc/simlin-engine/src/ast/expr0.rs,src/simlin-engine/src/ast/expr2.rs--Constfloat equality, the root
Possible approach
The clean fix is at the root: compare the f64 by bit pattern (f64::to_bits) inside Expr0/Expr2 equality, so NaN == NaN and a bit-identical AST backdates. That single change fixes this issue, #981, and #642 together, since all three are the same f64-in-a-derived-PartialEq root. It does change equality semantics for +0.0 vs -0.0 (they stop comparing equal), which needs its own analysis -- for salsa's purposes "different bits therefore recompute" is conservative and safe, but any other PartialEq consumer of these ASTs should be checked.
Note for whoever does it: salsa::Update is an unsafe trait, and its derive compares fields independently, so hand-writing only PartialEq is insufficient -- the Update impl must agree, and getting maybe_update wrong makes salsa MISS real changes (stale results, strictly worse than the perf issue being fixed). See #981, which measured <LtmEquation as salsa::Update>::maybe_update(..) returning CHANGED for identical NaN-bearing input.
Relationship to existing issues (not duplicates)
- ltm: a NaN literal in a scored equation defeats salsa backdating for typed LtmEquation (avoidable fragment recompiles) #981 -- same root cause, applied to
LtmArm/LtmEquationin the LTM subsystem. Its reachability analysis concludes the defect is narrow ("it needs aNaNliteral in a user equation that feeds a scored LTM link... A hand-written XMILE equation usingNaNis the realistic path"). That conclusion does not carry over here: the stdlib templates put NaN into every project's stage values unconditionally. - engine: salsa::Update on compiled bytecode uses PartialEq over f64/NaN (benign missed-backdate) #642 -- same root cause, applied to the compiled bytecode types (
ByteCode.literals,ByteCodeContext.graphical_functions,vm::Specs). Argued benign there because the only consumer is non-tracked. Also does not carry over:model_stage0/model_stage1/parse_source_variable_with_module_contextare all tracked, and each is read by other tracked queries.
Filed separately from #981 rather than as a comment on it because the affected types, the component, and above all the reachability argument are different; the fix is plausibly shared, which is what the "possible approach" section above records.
Severity
Low today (performance/incrementality only, latent), but it is a latent cliff on the interactive diagnostics path rather than a cosmetic one, and it already costs a test its natural fixture.
Context
Identified while implementing #966 (caching ModelStage0/ModelStage1 as salsa-tracked queries) on branch roundtrips-track-b.
Activity