sva-engine 0.8.0

Renders a resolved graph into per-node buffers a query can be asked of
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// Concern: the version a render or stream plays, advanced per edit, and its walk to what memory answers | Non-concern: the value graph | IO: (an edit) -> a version; (a root) -> hits; commit, abort

use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;

use sva_ast::{Expr, Graph, Held};
use sva_formula::Hash;

use sva_samples::Extent;

use super::terms::{Handle, NOTES, Terms, is_term};
use super::{RenderConfig, default_end, default_start};
use crate::cache::{Known as Answer, Stored};
use crate::error::{Diagnostic, EngineError, Located};
use crate::instantiate::Instances;
use crate::query::Representation;
use crate::schedule;
use crate::typing::Typing;

pub const STREAMED: &str = "streamed";

pub(super) type Found<'f> = &'f mut dyn FnMut(&str, Hash) -> Answer;

pub(super) struct World {
    pub(super) graph: Graph,
    pub(super) instances: Instances,
    pub(super) typing: Typing,
    known: BTreeMap<String, Known>,
    edits: Vec<(String, Option<Held>)>,
    /// Whether it writes `@notes` as the sum of its terms: a stream's, over a composition that
    /// defines none.
    pub(super) notes: bool,
    replaced: Option<Instances>,
}

#[derive(Clone)]
pub(super) struct Known {
    /// What it computes; none where its identity refuses.
    identity: Option<Hash>,
    group: Arc<[String]>,
    /// A loop's member, a term, a reader of the note sum or one with no identity: never looked up.
    pinned: bool,
    reads_notes: bool,
    looked: bool,
}

/// The node a version plays: a stream's expression, held as its own node, or a node of the graph.
pub(super) enum Root<'a> {
    Streamed(&'a Expr),
    Node(&'a str),
}

pub(super) struct Wanted<'a> {
    pub(super) root: Root<'a>,
    pub(super) terms: &'a Terms,
    /// The term it adds, replaces or cuts, as it now stands.
    pub(super) term: Option<(Handle, &'a Expr)>,
    /// A graph reaching what it reads, and its reads.
    pub(super) from: Option<(&'a Graph, Vec<String>)>,
    /// Whether `from` holds all there is, so a node it lacks is gone.
    pub(super) whole: bool,
}

pub(super) struct Advance {
    adopted: usize,
    named: Vec<String>,
    found: BTreeMap<String, Known>,
    changed: BTreeSet<String>,
    fresh: BTreeMap<String, BTreeSet<String>>,
}

pub(super) struct Plan {
    pub(super) adopted: usize,
    pub(super) named: usize,
    pub(super) visited: usize,
    pub(super) stored: BTreeMap<String, Arc<Stored>>,
    pub(super) found: BTreeMap<String, Known>,
}

pub(super) enum Walked {
    Asks(Vec<Hash>),
    Planned(Plan),
}

impl World {
    /// `graph` as it stands, played at `rate`; `notes` where it writes `@notes` from its terms.
    pub(super) fn over(graph: &Graph, rate: u32, notes: bool) -> World {
        World {
            graph: graph.clone(),
            instances: Instances::new(rate),
            typing: Typing::default(),
            known: BTreeMap::new(),
            edits: Vec::new(),
            notes,
            replaced: None,
        }
    }

    /// `target` of `graph`, at `rate`, the version in `world` advanced to it and held: only
    /// what changed since, and its readers, typed anew. Its root instance; on a refusal the
    /// version is as it was.
    pub(super) fn rendered<'w>(
        world: &'w mut Option<World>,
        graph: &Graph,
        target: &str,
        rate: u32,
    ) -> Result<(&'w World, String), EngineError> {
        let world = match world.take() {
            Some(held) if held.instances.rate() == rate => world.insert(held),
            _ => world.insert(World::over(graph, rate, false)),
        };
        let wanted = Wanted {
            root: Root::Node(target),
            terms: &Terms::default(),
            term: None,
            from: Some((graph, vec![target.to_string()])),
            whole: true,
        };
        match world.advanced(&wanted) {
            Ok(advance) => {
                world.commit(advance.found);
                let root = world.instances.instance_of(target)?;
                Ok((world, root))
            }
            Err(refused) => {
                world.abort();
                Err(refused)
            }
        }
    }

    /// The graph set to what `wanted` plays, what changed named, scanned and typed, each
    /// instance it may rename named by what it computes, and a walk to what the store answers
    /// of each changed or newly read. Undone on a refusal or keys to look up.
    pub(super) fn plan(
        &mut self,
        wanted: &Wanted,
        config: &RenderConfig,
        found: Found,
    ) -> Result<Walked, EngineError> {
        let planned = self
            .advanced(wanted)
            .map(|advance| self.walked(advance, config, found));
        match &planned {
            Ok(Walked::Planned(_)) => {}
            _ => {
                self.abort();
            }
        }
        planned
    }

    fn advanced(&mut self, wanted: &Wanted) -> Result<Advance, EngineError> {
        let root = match wanted.root {
            Root::Streamed(_) => STREAMED,
            Root::Node(path) => path,
        };
        let held: Vec<&String> = self.instances.own_terms.keys().collect();
        let moved = !held.is_empty() && held != [root];
        let adopted = match &wanted.from {
            Some((graph, roots)) => {
                self.renew(graph, roots, (moved, wanted.whole));
                self.adopt(graph, roots)
            }
            None => 0,
        };
        let mut rewritten = Vec::new();
        if let Root::Streamed(target) = wanted.root {
            self.set(STREAMED, target, &mut rewritten);
        }
        if !wanted.terms.is_empty() && !self.notes {
            return Err(EngineError::refused(Diagnostic {
                code: "engine.no_stream".to_string(),
                message: format!(
                    "a term is added to `@{NOTES}`, and this composition defines its own `{NOTES}`"
                ),
                location: Located::at(NOTES, None),
                help: format!(
                    "rename the composition's `{NOTES}`, or play it without adding terms"
                ),
            }));
        }
        let first = !self.instances.own_terms.contains_key(root);
        if let Some((handle, term)) = wanted.term {
            self.set(&handle.node(), term, &mut rewritten);
        }
        if self.notes {
            self.set(NOTES, &wanted.terms.sum(), &mut rewritten);
        }
        match first {
            true => self
                .instances
                .hold(&self.graph, &[root.to_string()])
                .map(|_| ())?,
            false => self.instances.rewrite(&self.graph, &rewritten)?,
        }
        let (named, rescanned) = self.instances.changing();
        let rescanned = rescanned.map(|(n, before)| (n.to_string(), before.to_vec()));
        let (mut named, rescanned): (Vec<String>, Vec<(String, Vec<String>)>) =
            (named.to_vec(), rescanned.collect());
        if let Some(old) = &self.replaced {
            let held = |path: &String| old.resolution(path) == self.instances.resolution(path);
            named.retain(|path| !held(path));
        }
        let region = self.region(&named, &rescanned);
        self.typing.lower(&self.instances, &region)?;
        if self.notes {
            wanted.terms.name(&mut self.typing);
        }
        let (found, changed) = self.named(&region);
        let mut fresh: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
        for name in &named {
            let deps = self.instances.deps(name).iter().cloned();
            fresh.insert(name.clone(), deps.collect());
        }
        for (name, before) in &rescanned {
            let deps = self.instances.deps(name).iter();
            let new = deps.filter(|d| !before.contains(d)).cloned();
            fresh.insert(name.clone(), new.collect());
        }
        Ok(Advance {
            adopted,
            named,
            found,
            changed,
            fresh,
        })
    }

    fn walked(&self, advance: Advance, config: &RenderConfig, found: Found) -> Walked {
        let walking = Walking {
            root: STREAMED,
            config,
            whole: false,
        };
        let reached = match self.reach(&walking, Some(&advance), found) {
            Reach::Asks(keys) => return Walked::Asks(keys),
            Reach::Reached(reached) => reached,
        };
        Walked::Planned(Plan {
            adopted: advance.adopted,
            named: advance.named.len(),
            visited: reached.visited.len(),
            stored: reached.held,
            found: reached.known,
        })
    }

    /// A walk down from `walking`'s root to what memory answers, through every miss: a
    /// render's whole, or a change's to each node it changed or newly reads.
    pub(super) fn walk(&self, walking: &Walking, found: Found) -> Reach {
        self.reach(walking, None, found)
    }

    fn reach(&self, walking: &Walking, change: Option<&Advance>, found: Found) -> Reach {
        let walk = Walk {
            world: self,
            known: change.map_or_else(BTreeMap::new, |c| c.found.clone()),
            changed: change.map(|c| &c.changed),
            fresh: change.map(|c| &c.fresh),
            pins: Pins::of(&self.instances, walking.config),
            walking,
            found,
        };
        walk.walked()
    }

    /// What memory holds `known`'s own value under; none where its identity refuses.
    fn keyed(&self, path: &str, known: &Known, config: &RenderConfig) -> Option<Hash> {
        let id = self.typing.id(path)?;
        let identity = known.identity?;
        Some(super::value_graph::node_key(
            &self.typing,
            id,
            identity,
            &config.profile,
        ))
    }

    fn set(&mut self, path: &str, expr: &Expr, rewritten: &mut Vec<String>) {
        if self.graph.expr(path) == Some(expr) {
            return;
        }
        let held = self.graph.defining(expr.clone());
        let before = self.graph.set(path, Some(held));
        self.edits.push((path.to_string(), before));
        rewritten.push(path.to_string());
    }

    /// Each node played or read anew that `from` holds otherwise than the graph, taken into
    /// it; where a parse moved, or the root `moved`, the instances are named anew, whole,
    /// beside the ones set aside.
    fn renew(&mut self, from: &Graph, roots: &[String], (moved, whole): (bool, bool)) {
        let read = from.reaching(roots);
        let played = |p: &str| self.instances.instances_of(p).next().is_some() || read.contains(p);
        let gone = self.graph.paths().filter(|p| whole && !from.defines(p));
        let paths = from.paths().filter(|p| self.graph.defines(p)).chain(gone);
        let held = |p: &&str| self.graph.holds_as(p, from) && self.graph.read_as(p, from);
        let paths = paths.filter(|p| played(p) && !held(p));
        let taken: Vec<String> = paths.map(str::to_string).collect();
        let reparsed = taken.iter().any(|p| !self.graph.holds_as(p, from));
        for path in taken {
            let before = self.graph.set(&path, from.held(&path));
            self.edits.push((path, before));
        }
        if !reparsed && !moved {
            return;
        }
        let fresh = Instances::new(self.instances.rate());
        self.replaced = Some(std::mem::replace(&mut self.instances, fresh));
    }

    fn adopt(&mut self, from: &Graph, roots: &[String]) -> usize {
        let taken = self.graph.adopt(from, roots);
        let count = taken.len();
        self.edits
            .extend(taken.into_iter().map(|path| (path, None)));
        count
    }

    /// What a change may rename: what it named and scanned again, their old groups and all
    /// reading them, grouped dependencies first.
    fn region(&self, named: &[String], rescanned: &[(String, Vec<String>)]) -> Vec<Vec<String>> {
        let mut region: BTreeSet<String> = named.iter().cloned().collect();
        for (name, _) in rescanned {
            region.insert(name.clone());
            if let Some(known) = self.known.get(name) {
                region.extend(known.group.iter().cloned());
            }
        }
        let mut up: Vec<String> = region.iter().cloned().collect();
        while let Some(at) = up.pop() {
            for reader in self.instances.readers(&at) {
                if region.insert(reader.to_string()) {
                    up.push(reader.to_string());
                }
            }
        }
        let starts: Vec<String> = region.iter().cloned().collect();
        schedule::grouped(&self.instances, &starts, &|path| region.contains(path))
    }

    /// Each member of the region named by what its typing computes, and those whose identity
    /// moved.
    fn named(&self, region: &[Vec<String>]) -> (BTreeMap<String, Known>, BTreeSet<String>) {
        let mut found: BTreeMap<String, Known> = BTreeMap::new();
        let mut changed = BTreeSet::new();
        for group in region {
            let looped = schedule::is_loop(&self.instances, group);
            let reads_notes = self.notes
                && group.iter().any(|path| {
                    path == NOTES
                        || self.instances.deps(path).iter().any(|read| {
                            let known = found.get(read).or_else(|| self.known.get(read));
                            read == NOTES || known.is_some_and(|k| k.reads_notes)
                        })
                });
            let members: Arc<[String]> = group.clone().into();
            for path in group {
                let typed = self.typing.id(path);
                let identity = typed.and_then(|id| crate::refs::identity(&self.typing, id).ok());
                let old = self
                    .known
                    .get(path)
                    .filter(|old| identity.is_some() && old.identity == identity);
                if old.is_none() {
                    changed.insert(path.clone());
                }
                let pinned =
                    identity.is_none() || reads_notes || looped || (self.notes && is_term(path));
                let known = Known {
                    identity,
                    group: Arc::clone(&members),
                    pinned,
                    reads_notes,
                    looked: old.is_some_and(|old| old.looked),
                };
                found.insert(path.clone(), known);
            }
        }
        (found, changed)
    }

    /// What nothing reads let go; the typing's freed ids.
    pub(super) fn commit(&mut self, found: BTreeMap<String, Known>) -> Vec<sva_formula::NodeId> {
        let mut removed = self.instances.commit();
        if let Some(old) = self.replaced.take() {
            let gone = old.paths().filter(|p| !self.instances.holds(p));
            removed.extend(gone.map(str::to_string));
        }
        self.typing.hide(removed.iter().cloned());
        let freed = self.typing.commit(&self.instances);
        self.known.extend(found);
        for path in &removed {
            self.known.remove(path);
            if is_term(path) && self.notes {
                self.graph.set(path, None);
            }
        }
        self.edits.clear();
        freed
    }

    /// Everything since the last commit undone; the draft typing's ids.
    pub(super) fn abort(&mut self) -> Vec<sva_formula::NodeId> {
        let freed = self.typing.abort();
        self.instances.abort();
        if let Some(old) = self.replaced.take() {
            self.instances = old;
        }
        for (path, before) in std::mem::take(&mut self.edits).into_iter().rev() {
            self.graph.set(&path, before);
        }
        freed
    }
}

/// How a walk asks memory: from which root, at which rate and profile, for which readings.
pub(super) struct Walking<'w> {
    pub(super) root: &'w str,
    pub(super) config: &'w RenderConfig,
    /// A render's: every node from the root looked up anew, the root answering over its range.
    pub(super) whole: bool,
}

pub(super) struct Reached {
    pub(super) visited: BTreeSet<String>,
    pub(super) held: BTreeMap<String, Arc<Stored>>,
    known: BTreeMap<String, Known>,
}

pub(super) enum Reach {
    Asks(Vec<Hash>),
    Reached(Reached),
}

/// What the readings asked of a walk pin: every node under a ledger, a node a reading asks
/// more of than samples, and every node above one a reading asks of, which a hit would hide.
struct Pins {
    all: bool,
    pinned: BTreeSet<String>,
    asked: BTreeMap<String, Vec<Representation>>,
}

impl Pins {
    fn of(inst: &Instances, config: &RenderConfig) -> Pins {
        let ledger =
            |ask: &&crate::query::Ask| matches!(ask.representation, Representation::Ledger { .. });
        let mut pins = Pins {
            all: config.asks.iter().any(|ask| ledger(&ask)),
            pinned: BTreeSet::new(),
            asked: BTreeMap::new(),
        };
        for ask in &config.asks {
            let Ok(path) = inst.instance_of(&ask.node) else {
                continue;
            };
            if !ask.representation.off_samples(true) {
                pins.pinned.insert(path.clone());
            }
            pins.asked.entry(path).or_default().push(ask.representation);
        }
        let mut up: Vec<&str> = pins.asked.keys().map(String::as_str).collect();
        while let Some(at) = up.pop() {
            for reader in inst.readers(at) {
                if pins.pinned.insert(reader.to_string()) {
                    up.push(reader);
                }
            }
        }
        pins
    }

    fn holds(&self, path: &str) -> bool {
        self.all || self.pinned.contains(path)
    }
}

struct Walk<'w> {
    world: &'w World,
    known: BTreeMap<String, Known>,
    changed: Option<&'w BTreeSet<String>>,
    fresh: Option<&'w BTreeMap<String, BTreeSet<String>>>,
    pins: Pins,
    walking: &'w Walking<'w>,
    found: Found<'w>,
}

struct Visit(String, bool);

impl Walk<'_> {
    fn known(&self, path: &str) -> Known {
        let known = self.known.get(path).or_else(|| self.world.known.get(path));
        known.cloned().expect("an instance named")
    }

    fn walked(mut self) -> Reach {
        let (mut visited, mut asks) = (BTreeSet::new(), Vec::new());
        let mut held: BTreeMap<String, Arc<Stored>> = BTreeMap::new();
        let (config, whole) = (self.walking.config, self.walking.whole);
        let mut stack = vec![Visit(self.walking.root.to_string(), false)];
        while let Some(Visit(path, anew)) = stack.pop() {
            let mut known = self.known(&path);
            let pinned = known.pinned || self.pins.holds(&path);
            let key = self.world.keyed(&path, &known, config).filter(|_| !pinned);
            if visited.contains(&path) {
                let asked = anew && !held.contains_key(&path);
                if let Some(key) = key.filter(|_| asked)
                    && matches!((self.found)(&path, key), Answer::Unknown)
                {
                    asks.push(key);
                }
                continue;
            }
            let changed = self.changed.is_some_and(|c| c.contains(&path));
            if !(whole || anew || !known.looked || changed) {
                continue;
            }
            visited.insert(path.clone());
            let stored = match key {
                None => None,
                Some(key) => match (self.found)(&path, key) {
                    Answer::Hit(hit) => Some((key, hit)),
                    Answer::Miss => None,
                    Answer::Unknown => {
                        asks.push(key);
                        continue;
                    }
                },
            };
            let root = whole && path == self.walking.root;
            let read = self.pins.asked.get(&path).map_or(&[][..], Vec::as_slice);
            let answering = |hit: &Arc<Stored>| {
                answers(hit, root, config) && read.iter().all(|r| r.off_samples(hit.sampled))
            };
            known.looked = true;
            match stored.filter(|(_, hit)| answering(hit)) {
                Some((_, stored)) => {
                    held.insert(path.clone(), stored);
                }
                None => {
                    let fresh = self.fresh.and_then(|f| f.get(&path));
                    for read in self.world.instances.deps(&path).iter().rev() {
                        let anew = fresh.is_some_and(|f| f.contains(read));
                        stack.push(Visit(read.clone(), anew));
                    }
                }
            }
            self.known.insert(path, known);
        }
        if !asks.is_empty() {
            asks.sort();
            asks.dedup();
            return Reach::Asks(asks);
        }
        Reach::Reached(Reached {
            visited,
            held,
            known: self.known,
        })
    }
}

/// A render's root answers where it holds the samples read; any other node, or a stream's
/// node, where a reader may take its samples.
fn answers(hit: &Stored, root: bool, config: &RenderConfig) -> bool {
    let support = hit.support;
    if !root {
        return hit.readable;
    }
    let start = config.range.start.unwrap_or_else(|| default_start(support));
    let Some(end) = config.range.end.or(default_end(support)) else {
        return false;
    };
    hit.holds(Extent::new(start, end.max(start)).intersect(support))
}