devpage-design 0.2.5

The design engine: parses, resolves, lays out and paints devpage designs
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
//! What an edit costs by the kind of frame it is in: a list's widest row
//! made wider, a list's own fill changed, a child of a frame with no layout
//! resized or inserted first, a sibling of a `"fill"` child resized, and a
//! child of a `"space-between"` frame resized — each at 1,000, 10,000 and
//! 100,000 siblings, the save resolved again and the drawing brought across.

use crate::ids::Remembering;
use std::collections::HashMap;

use crate::render::Drawing;
use crate::resolve::{Outcome, Resolution};
use crate::scene::{Bare, Theme};

use super::super::*;

/// A design of `n` siblings as `kind` lays them out, the `edit`ed one at
/// `n / 2` (or a new one first) changed.
pub(super) fn source(kind: Kind, n: usize, edited: bool) -> String {
    let mut rows = String::new();
    if edited && kind == Kind::NoLayoutFront {
        rows.push_str("<Rect x={0} y={0} width={50} height={10} fill=\"#000000\" />");
    }
    for i in 0..n {
        let e = edited && i == n / 2;
        rows.push_str(&match kind {
            Kind::Widest | Kind::ParentFill => format!(
                "<Frame layout=\"row\" padding={{4}}><Text>{}</Text></Frame>",
                if e && kind == Kind::Widest {
                    "Edited and a good deal longer".to_string()
                } else {
                    format!("Row {i}")
                }
            ),
            Kind::NoLayoutResize | Kind::NoLayoutFront => format!(
                "<Rect x={{0}} y={{{}}} width={{{}}} height={{8}} fill=\"#336699\" />",
                i * 9,
                if e && kind == Kind::NoLayoutResize {
                    150
                } else {
                    100
                }
            ),
            Kind::FixedFree => format!(
                "<Frame x={{0}} y={{{}}} layout=\"column\" width={{{}}}><Text width=\"fill\">Row {i}</Text></Frame>",
                i * 9,
                if e { 300 } else { 200 }
            ),
            Kind::Align => format!(
                "<Frame layout=\"row\" padding={{4}}><Text>{}</Text></Frame>",
                "Row ".repeat(1 + i % 3)
            ),
            Kind::FillSibling | Kind::SpaceBetween => format!(
                "<Rect width={{100}} height={{{}}} fill=\"#336699\" />",
                if e { 3 } else { 2 }
            ),
        });
    }
    let fill_child = if kind == Kind::FillSibling {
        "<Rect width={100} height=\"fill\" fill=\"#000000\" />"
    } else {
        ""
    };
    let root = match kind {
        Kind::Widest => "<Frame layout=\"column\">".to_string(),
        Kind::ParentFill => format!(
            "<Frame layout=\"column\"{}>",
            if edited { " fill=\"#ff0000\"" } else { "" }
        ),
        Kind::NoLayoutResize | Kind::NoLayoutFront | Kind::FixedFree => "<Frame>".to_string(),
        Kind::Align => format!(
            "<Frame layout=\"column\"{}>",
            if edited { " align=\"center\"" } else { "" }
        ),
        Kind::FillSibling => format!("<Frame layout=\"column\" height={{{}}}>", n * 3 + 100),
        Kind::SpaceBetween => format!(
            "<Frame layout=\"column\" justify=\"space-between\" height={{{}}}>",
            n * 3 + 100
        ),
    };
    format!("{root}{rows}{fill_child}</Frame>")
}

#[derive(Clone, Copy, PartialEq, Debug)]
pub(super) enum Kind {
    Widest,
    ParentFill,
    NoLayoutResize,
    NoLayoutFront,
    FillSibling,
    SpaceBetween,
    /// A child of a frame with no layout, of a fixed width holding wrapping
    /// text, made wider: no sibling depends on the frame's new room.
    FixedFree,
    /// A list of rows of three widths centred: every row's box moves across,
    /// so every row is placed again — and only the rows, not what is in them.
    Align,
}

/// The kinds whose edit costs the nodes it changes and log time.
pub(super) const KINDS: [Kind; 7] = [
    Kind::Widest,
    Kind::ParentFill,
    Kind::NoLayoutResize,
    Kind::NoLayoutFront,
    Kind::FillSibling,
    Kind::SpaceBetween,
    Kind::FixedFree,
];

/// The save of `kind`'s edit at `n`: resolving it again, and the drawing's
/// update, each timed, and how many nodes the update visited. The drawing
/// it leaves, and a fresh one of the edited design.
pub(super) fn edit(
    kind: Kind,
    n: usize,
) -> (
    std::time::Duration,
    std::time::Duration,
    usize,
    Drawing,
    Drawing,
    Renderer,
) {
    let mut saves = Remembering::new(HashMap::<String, String>::new());
    saves
        .files
        .insert("a.design".into(), source(kind, n, false));
    let (mut res, scene) = Resolution::new(&saves, "a.design").unwrap();
    let mut held = Bare::new(scene);
    let mut r = Renderer::new();
    let mut d = r.drawing(&held.scene, Theme::Light, None).unwrap();
    saves.files.insert("a.design".into(), source(kind, n, true));
    let t = std::time::Instant::now();
    let Outcome::Changed(_, applied) = res.update(&mut held, &saves, &["a.design".to_string()])
    else {
        panic!("{kind:?}: the edit changed nothing");
    };
    let resolved = t.elapsed();
    let t = std::time::Instant::now();
    d.update(&mut r, &held.scene, &applied).unwrap();
    let updated = t.elapsed();
    let fresh = r.drawing(&held.scene, Theme::Light, None).unwrap();
    (resolved, updated, d.visited, d, fresh, r)
}

/// The numbers: `cargo test --release -p devpage-design frame_edit_costs --
/// --ignored --nocapture`.
#[test]
#[ignore]
fn frame_edit_costs() {
    let only = std::env::var("DEVPAGE_KINDS").unwrap_or_default();
    for kind in KINDS.into_iter().chain([Kind::Align]) {
        if !only.is_empty() && !only.contains(&format!("{kind:?}")) {
            continue;
        }
        for n in [1_000, 10_000, 100_000] {
            let (resolved, updated, visited, ..) = edit(kind, n);
            eprintln!(
                "{kind:?} {n:>7}: resolve again {resolved:?}, update {updated:?} ({visited} visited)"
            );
        }
    }
}

/// Every kind of edit visits the same nodes among 1,000 siblings as among
/// 10,000 — none of the siblings beside what it changed — and leaves a
/// drawing that lays out, paints and hit-tests as a fresh one.
#[test]
fn an_edit_costs_what_it_changed_in_every_kind_of_frame() {
    for kind in KINDS {
        let mut seen = None;
        for n in [1_000, 10_000] {
            let (_, _, visited, mut d, fresh, mut r) = edit(kind, n);
            assert!(visited <= 3, "{kind:?} at {n}: {visited} visited");
            if let Some(v) = seen {
                assert_eq!(visited, v, "{kind:?}: visits grow with the siblings");
            }
            seen = Some(visited);
            let scene = {
                let mut saves = Remembering::new(HashMap::<String, String>::new());
                saves
                    .files
                    .insert("a.design".into(), source(kind, n, false));
                let _ = crate::resolve(&saves, "a.design").unwrap();
                saves.files.insert("a.design".into(), source(kind, n, true));
                crate::resolve(&saves, "a.design").unwrap()
            };
            d.prepare_all(&mut r, &scene);
            assert_eq!(d.area(), fresh.area(), "{kind:?} at {n}");
            assert_eq!(d.frames(), fresh.frames(), "{kind:?} at {n}");
            let rows = (fresh.area().height as u32).div_ceil(crate::tiles::TILE);
            for row in [0, rows / 2, rows - 1] {
                assert!(
                    crate::cpu::tile(&d.list, 1.0, 0, row)
                        == crate::cpu::tile(&fresh.list, 1.0, 0, row),
                    "{kind:?} at {n}: tile row {row}"
                );
            }
        }
    }
}

/// A frame with no layout that hugs a wrapping text: a sibling that widens
/// it lays the text out again against the wider box, and the drawing shows
/// the text as a fresh one does.
#[test]
fn a_frame_that_grows_lays_out_again_what_its_box_reaches() {
    let design = |w: u32| {
        format!(
            "<Frame layout=\"column\"><Frame><Rect x={{0}} y={{0}} width={{{w}}} height={{10}} /><Frame x={{0}} y={{20}} layout=\"column\"><Text width=\"fill\">a few words that wrap at the frame</Text></Frame></Frame></Frame>"
        )
    };
    let mut saves = Remembering::new(HashMap::<String, String>::new());
    saves.files.insert("a.design".into(), design(60));
    let (mut res, scene) = Resolution::new(&saves, "a.design").unwrap();
    let mut held = Bare::new(scene);
    let mut r = Renderer::new();
    let mut d = r.drawing(&held.scene, Theme::Light, None).unwrap();
    saves.files.insert("a.design".into(), design(300));
    let Outcome::Changed(_, applied) = res.update(&mut held, &saves, &["a.design".to_string()])
    else {
        panic!("the edit changed nothing");
    };
    d.update(&mut r, &held.scene, &applied).unwrap();
    d.prepare_all(&mut r, &held.scene);
    let fresh = r.drawing(&held.scene, Theme::Light, None).unwrap();
    assert_eq!(d.frames(), fresh.frames());
}

/// A list's new `align` moves every row that doesn't fill its line: each is
/// placed again, its contents — in its own coordinates — are not visited,
/// and the drawing equals a fresh one in its boxes and its pixels.
#[test]
fn a_new_align_places_every_row_again_and_nothing_in_them() {
    for n in [1_000, 10_000] {
        let (_, _, visited, mut d, fresh, mut r) = edit(Kind::Align, n);
        assert!(visited <= n + 3, "at {n}: {visited} visited");
        let scene = {
            let mut saves = Remembering::new(HashMap::<String, String>::new());
            saves
                .files
                .insert("a.design".into(), source(Kind::Align, n, false));
            let _ = crate::resolve(&saves, "a.design").unwrap();
            saves
                .files
                .insert("a.design".into(), source(Kind::Align, n, true));
            crate::resolve(&saves, "a.design").unwrap()
        };
        d.prepare_all(&mut r, &scene);
        assert_eq!(d.area(), fresh.area(), "at {n}");
        assert_eq!(d.frames(), fresh.frames(), "at {n}");
        let rows = (fresh.area().height as u32).div_ceil(crate::tiles::TILE);
        for row in 0..rows.min(40) {
            assert!(
                crate::cpu::tile(&d.list, 1.0, 0, row)
                    == crate::cpu::tile(&fresh.list, 1.0, 0, row),
                "at {n}: tile row {row}"
            );
        }
    }
}

/// Every attribute of a list that places its children — `align`,
/// `justify`, `padding`, `gap`, its direction — changed on 1,000 rows of
/// three widths in a fixed box, each from every other value: the drawing
/// equals a fresh one in its boxes and its pixels.
#[test]
fn every_attribute_that_places_a_lists_children_redraws_as_a_fresh_drawing() {
    let attrs: &[&[&str]] = &[
        &["", " align=\"center\"", " align=\"end\""],
        &[
            "",
            " justify=\"center\"",
            " justify=\"end\"",
            " justify=\"space-between\"",
        ],
        &["", " padding={7}", " padding={[3, 11]}"],
        &["", " gap={5}"],
        &[" layout=\"column\"", " layout=\"row\""],
    ];
    let design = |extra: &str, layout: &str| {
        let rows: String = (0..1_000)
            .map(|i| {
                format!(
                    "<Frame layout=\"row\" padding={{2}}><Text>{}</Text></Frame>",
                    "Row ".repeat(1 + i % 3)
                )
            })
            .collect();
        format!("<Frame{layout} width={{60000}} height={{60000}}{extra}>{rows}</Frame>")
    };
    for values in attrs {
        for a in values.iter() {
            for b in values.iter().filter(|b| *b != a) {
                let (from, to, layout) = if values[0].contains("layout") {
                    ("", "", (*a, *b))
                } else {
                    (*a, *b, (" layout=\"column\"", " layout=\"column\""))
                };
                let mut saves = Remembering::new(HashMap::<String, String>::new());
                saves
                    .files
                    .insert("a.design".into(), design(from, layout.0));
                let (mut res, scene) = Resolution::new(&saves, "a.design").unwrap();
                let mut held = Bare::new(scene);
                let mut r = Renderer::new();
                let mut d = r.drawing(&held.scene, Theme::Light, None).unwrap();
                saves.files.insert("a.design".into(), design(to, layout.1));
                let Outcome::Changed(_, applied) =
                    res.update(&mut held, &saves, &["a.design".to_string()])
                else {
                    panic!("{a} → {b}: nothing changed");
                };
                d.update(&mut r, &held.scene, &applied).unwrap();
                d.prepare_all(&mut r, &held.scene);
                let fresh = r.drawing(&held.scene, Theme::Light, None).unwrap();
                assert_eq!(d.frames(), fresh.frames(), "{a} → {b}");
                for (col, row) in [(0, 0), (0, 2), (1, 1), (3, 3)] {
                    assert!(
                        crate::cpu::tile(&d.list, 1.0, col, row)
                            == crate::cpu::tile(&fresh.list, 1.0, col, row),
                        "{a} → {b}: tile {col},{row}"
                    );
                }
            }
        }
    }
}

/// How an edit's update grows with the siblings beside it, as the median of
/// 20 alternating saves at 10,000, 100,000 and 200,000: `cargo test --release
/// -p devpage-design edit_scaling -- --ignored --nocapture`. `DEVPAGE_LOOP`
/// repeats the largest, to profile it.
#[test]
#[ignore]
fn edit_scaling() {
    let row =
        |i: usize| format!("<Frame layout=\"row\" padding={{4}}><Text>Row {i}</Text></Frame>");
    type Source = Box<dyn Fn(usize, bool) -> String>;
    let cases: Vec<(&str, Source)> = vec![
        (
            "a middle row deleted",
            Box::new(move |n, e| {
                let rows: String = (0..n)
                    .map(|i| {
                        if e && i == n / 2 {
                            String::new()
                        } else {
                            row(i)
                        }
                    })
                    .collect();
                format!("<Frame layout=\"column\">{rows}</Frame>")
            }),
        ),
        (
            "the widest row made narrower",
            Box::new(move |n, e| {
                let rows: String = (0..n)
                    .map(|i| {
                        if i == n / 2 {
                            format!(
                                "<Frame layout=\"row\" padding={{4}}><Text>{}</Text></Frame>",
                                if e {
                                    "x"
                                } else {
                                    "The widest row of them all by far"
                                }
                            )
                        } else {
                            row(i)
                        }
                    })
                    .collect();
                format!("<Frame layout=\"column\">{rows}</Frame>")
            }),
        ),
        (
            "a child of a frame with no layout resized",
            Box::new(|n, e| source(Kind::NoLayoutResize, n, e)),
        ),
    ];
    let repeat: usize = std::env::var("DEVPAGE_LOOP")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(0);
    let only = std::env::var("DEVPAGE_CASE").unwrap_or_default();
    for (name, src) in cases
        .iter()
        .filter(|(name, _)| name.contains(only.as_str()))
    {
        // A frame with no layout places its 200,000th child past the
        // largest number a design may write: it stops at 100,000.
        let lengths: &[usize] = if name.contains("no layout") {
            &[10_000, 50_000, 100_000]
        } else {
            &[10_000, 100_000, 200_000]
        };
        for &n in lengths {
            let mut saves = Remembering::new(HashMap::<String, String>::new());
            saves.files.insert("a.design".into(), src(n, false));
            let (mut res, scene) = Resolution::new(&saves, "a.design").unwrap();
            let mut held = Bare::new(scene);
            let mut r = Renderer::new();
            let mut d = r.drawing(&held.scene, Theme::Light, None).unwrap();
            let rounds = if n == *lengths.last().expect("a length") {
                20 + repeat
            } else {
                20
            };
            let mut times = vec![];
            for i in 0..rounds {
                saves.files.insert("a.design".into(), src(n, i % 2 == 0));
                let Outcome::Changed(_, applied) =
                    res.update(&mut held, &saves, &["a.design".to_string()])
                else {
                    panic!("{name}: nothing changed")
                };
                let t = std::time::Instant::now();
                d.update(&mut r, &held.scene, &applied).unwrap();
                times.push(t.elapsed());
            }
            times.sort();
            eprintln!(
                "{name} {n:>7}: median {:?}, least {:?} ({} visited)",
                times[times.len() / 2],
                times[0],
                d.visited
            );
        }
    }
}

/// One update and its undo, applied to the same drawing over and over with
/// nothing between, to profile: `DEVPAGE_LOOP` rounds of a middle row
/// deleted and put back among 200,000.
#[test]
#[ignore]
fn profile_update() {
    let n = 200_000;
    let src = |e: bool| {
        let rows: String = (0..n)
            .map(|i| {
                if e && i == n / 2 {
                    String::new()
                } else {
                    format!("<Frame layout=\"row\" padding={{4}}><Text>Row {i}</Text></Frame>")
                }
            })
            .collect();
        format!("<Frame layout=\"column\">{rows}</Frame>")
    };
    let mut saves = Remembering::new(HashMap::<String, String>::new());
    saves.files.insert("a.design".into(), src(false));
    let (mut res, scene) = Resolution::new(&saves, "a.design").unwrap();
    let mut held = Bare::new(scene);
    let a = held.scene.clone();
    saves.files.insert("a.design".into(), src(true));
    let Outcome::Changed(_, there) = res.update(&mut held, &saves, &["a.design".to_string()])
    else {
        panic!()
    };
    let b = held.scene.clone();
    saves.files.insert("a.design".into(), src(false));
    let Outcome::Changed(_, back) = res.update(&mut held, &saves, &["a.design".to_string()]) else {
        panic!()
    };
    let a2 = held.scene.clone();
    let mut r = Renderer::new();
    let mut d = r.drawing(&a, Theme::Light, None).unwrap();
    let rounds: usize = std::env::var("DEVPAGE_LOOP")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(10);
    eprintln!("drawn");
    let t = std::time::Instant::now();
    for _ in 0..rounds {
        d.update(&mut r, &b, &there).unwrap();
        d.update(&mut r, &a2, &back).unwrap();
    }
    eprintln!("{:?} a round", t.elapsed() / rounds as u32);
}