perf: a pass over the joints evaluates the design's parameters once (#6418)
Every joint whose offset, angle or values are expressions is worked out again on each solve, twice per solve (re-expressing the joints, then the check that leaves out one whose expression no longer works out), and each expression that names a parameter evaluated every parameter of the design anew (`Document::eval`). The cost grows with the joints with expressions times the design's parameters: on 40 parts jointed to a plate, each joint lifted by an expression and each part carrying 13 parameters (522 in all), a joint solve took 0.55 s on main and takes 0.41 s now, a change of the lifts' parameter 1.06 s and 0.81 s (CLI, medians of 5 runs). `joints::Params` keeps the design's parameter values once the first expression names one, and `joints::expressed` takes it, so one pass evaluates the parameters once. Values and error messages are those of `Document::eval`. Test: `kept_parameters_evaluate_as_the_design_does` compares values and errors with `Document::eval`. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -118,6 +118,7 @@ pub fn place(s: &mut Session) -> joints::Solution {
|
||||
/// may have changed). One that can't be worked out (its parameter gone) keeps its numbers; the
|
||||
/// solve leaves that joint out and says why.
|
||||
fn reexpress(s: &mut Session) {
|
||||
let params = joints::Params::new(&s.doc);
|
||||
let changed: Vec<(usize, (f64, f64, Vec<f64>))> = s
|
||||
.doc
|
||||
.assembly
|
||||
@@ -126,7 +127,7 @@ fn reexpress(s: &mut Session) {
|
||||
.enumerate()
|
||||
.filter(|(_, j)| !j.exprs.is_empty())
|
||||
.filter_map(|(i, j)| {
|
||||
let (o, a, mut q) = joints::expressed(&s.doc, j).ok()?;
|
||||
let (o, a, mut q) = joints::expressed(¶ms, j).ok()?;
|
||||
joints::clamp(j, &mut q);
|
||||
((o, a) != (j.offset, j.angle) || q != j.values).then_some((i, (o, a, q)))
|
||||
})
|
||||
|
||||
@@ -169,12 +169,13 @@ fn inputs(s: &Session, t: &MotionTrace) -> Result<Value> {
|
||||
let (occurrences, ids) = graph(s, t);
|
||||
let st = s.model.state();
|
||||
let seeds: BTreeMap<u64, &Vec<f64>> = t.seeds.iter().map(|(id, q)| (*id, q)).collect();
|
||||
let params = joints::Params::new(&s.doc);
|
||||
let mut joints = Vec::new();
|
||||
for j in s.doc.assembly.joints.iter().filter(|j| ids.contains(&j.id)) {
|
||||
let mut definition = j.clone();
|
||||
definition.name.clear();
|
||||
definition.values = seeds.get(&j.id).map(|q| (*q).clone()).unwrap_or_default();
|
||||
let (offset, angle, q) = joints::expressed(&s.doc, &definition)?;
|
||||
let (offset, angle, q) = joints::expressed(¶ms, &definition)?;
|
||||
definition.offset = offset;
|
||||
definition.angle = angle;
|
||||
definition.values = q;
|
||||
|
||||
Reference in New Issue
Block a user