gpui-fast 0.1.5

gpui: GPUI with gpui-fast rendering and layout optimizations
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
// Modified by Longbridge for gpui-fast.
//! Taffy layout nodes retained across frames, and the style fingerprint that decides whether a retained node can be left alone.

use crate::{
    AbsoluteLength, App, AvailableSpace, DefiniteLength, Edges, GridTemplate, LayoutId, Length,
    Pixels, Size, Style, TaffyLayoutEngine, Window,
    fast::stats::LayoutStats,
    taffy::{MeasureFn, NodeContext, ToTaffy as _},
    util::round_to_device_pixel,
};
use collections::{FxHashMap, FxHasher};
use smallvec::SmallVec;
use std::{
    any::Any,
    cell::{Cell, RefCell},
    fmt::Debug,
    hash::{Hash as _, Hasher as _},
    mem,
    rc::Rc,
};
use taffy::TaffyTree;

/// What a Taffy call that cannot fail by construction panics with, as
/// upstream's layout engine does.
const EXPECT_MESSAGE: &str = "we should avoid taffy layout errors by construction if possible";

/// The retained half of a [`TaffyLayoutEngine`]: the nodes it keeps from one
/// frame to the next, what it needs to tell whether a frame can leave them
/// alone, and the counters describing the work it did.
#[derive(Default)]
pub(crate) struct LayoutRetention {
    /// Nodes surviving from earlier frames, keyed by their position in the
    /// element tree. `push_layout_key` derives the keys.
    retained: FxHashMap<u64, RetainedNode>,
    /// Nodes allocated this frame that are not retained, because the caller had
    /// no key for them or because their key was already claimed. Released at
    /// the end of the frame, which is what used to happen to every node.
    transient: Vec<LayoutId>,
    /// Styles as the element requested them, for the few nodes whose style
    /// Taffy no longer holds verbatim because [`TaffyLayoutEngine::stretch_auto_size_to_fill`]
    /// rewrote it. Without this, every frame would compare an unstretched
    /// request against a stretched style, find a difference, and dirty the
    /// window root.
    unstretched_styles: FxHashMap<LayoutId, taffy::style::Style>,
    /// Incremented once per frame; stamped onto nodes as they are claimed.
    frame: u64,
    /// How many retained entries have been claimed so far this frame. When it
    /// matches the size of `retained` at the end of the frame, nothing has been
    /// orphaned and the sweep can be skipped entirely.
    claimed_this_frame: usize,
    /// The keys claimed while [`TaffyLayoutEngine::record_claimed_keys`] is
    /// recording, for a retained subtree to keep its nodes by. See
    /// [`crate::fast::retained`].
    claimed_key_log: Vec<u64>,
    /// How many recordings are open, nested retained subtrees each having one.
    open_key_recordings: usize,
    pub(crate) stats: LayoutStats,
    /// Whether to time layout and measurements. See [`LayoutStats`].
    pub(crate) timed: bool,
    /// Counts every write that changes what a layout computes: a node created,
    /// a style or a child list rewritten, a measurement that no longer
    /// stands. A subtree laid out again without any of these has the layout
    /// it had. See [`TaffyLayoutEngine::layout_changes`].
    layout_changes: u64,
    /// Counts the measured nodes given a new measurement to take. Their
    /// measurement has to be taken again, since what it produces lives in
    /// state the element made afresh, though it is expected to come out as
    /// before. See [`TaffyLayoutEngine::remeasures`].
    remeasures: u64,
    /// The [`layout_fingerprint`] of the default style, which every text leaf
    /// asks for, under the rem size and scale factor it was taken at.
    default_fingerprint: Option<(Pixels, f32, u64)>,
    /// The leaves the layout being computed measured, kept between
    /// computations so that its table is not allocated for each one. See
    /// [`settle_measured_leaves`].
    pub(crate) measured_leaves: MeasuredLeaves,
    /// Measured nodes given a new closure this frame and left clean for what
    /// their [`MeasureLog`] vouches for, to be measured with it before they
    /// are laid out. See [`replay_pending_measures`].
    pending_replays: Vec<(LayoutId, Rc<MeasureLog>)>,
    /// How many list rows are being laid out, nested. See
    /// [`RetainedNode::lingers`].
    list_rows: usize,
}

/// How many frames a node of a list row is kept unclaimed before it is
/// released. See [`RetainedNode::lingers`].
const LIST_ROW_NODE_LINGER_FRAMES: u64 = 240;

/// The leaves a layout computation measured, each with the width it was last
/// measured at: the width it was given, or the definite width it was offered,
/// or none when it was asked for its content size.
#[derive(Default)]
pub(crate) struct MeasuredLeaves(FxHashMap<taffy::NodeId, Option<Pixels>>);

impl MeasuredLeaves {
    /// Records that Taffy measured `node` under these constraints, replacing
    /// whatever it was measured at before in this computation.
    #[inline]
    pub(crate) fn note(
        &mut self,
        node: taffy::NodeId,
        known_dimensions: Size<Option<Pixels>>,
        available_space: Size<AvailableSpace>,
    ) {
        let width = known_dimensions.width.or(match available_space.width {
            AvailableSpace::Definite(width) => Some(width),
            AvailableSpace::MinContent | AvailableSpace::MaxContent => None,
        });
        self.0.insert(node, width);
    }
}

/// Measures again, at the size it was laid out at, every leaf whose last
/// measurement in the layout just computed was taken at another width.
///
/// A measured leaf keeps what its last measurement produced, and that is what
/// it paints: text paints the lines it was last shaped into. Upstream lays
/// every node out afresh each frame, and Taffy's last question to a leaf is the
/// one that places it, at the width it ends up with. A retained node keeps the
/// layout Taffy placed it at, and Taffy answers that question from its cache,
/// while it can still ask the leaf for its size under other constraints when a
/// node above it is laid out again. Two such questions can even take turns
/// evicting each other from the one cache slot they share, so the leaf is
/// measured again each time it is asked. Its last measurement is then a probe:
/// a `flex_1` text whose row is sized to its content is measured at width zero,
/// and painted one glyph to a line in the box it was laid out in.
pub(crate) fn settle_measured_leaves(
    engine: &mut TaffyLayoutEngine,
    window: &mut Window,
    cx: &mut App,
) {
    let mut leaves = mem::take(&mut engine.retention.measured_leaves);
    let scale_factor = window.scale_factor();
    for (node, width) in leaves.0.drain() {
        let Ok(layout) = engine.taffy.layout(node) else {
            continue;
        };
        let content = Size {
            width: Pixels(
                (layout.size.width
                    - layout.padding.left
                    - layout.padding.right
                    - layout.border.left
                    - layout.border.right)
                    .max(0.)
                    / scale_factor,
            ),
            height: Pixels(
                (layout.size.height
                    - layout.padding.top
                    - layout.padding.bottom
                    - layout.border.top
                    - layout.border.bottom)
                    .max(0.)
                    / scale_factor,
            ),
        };
        if width == Some(content.width) {
            continue;
        }
        let Some(context) = engine.taffy.get_node_context_mut(node) else {
            continue;
        };
        engine.retention.stats.measure_calls += 1;
        (context.measure)(
            Size {
                width: Some(content.width),
                height: Some(content.height),
            },
            Size {
                width: AvailableSpace::Definite(content.width),
                height: AvailableSpace::Definite(content.height),
            },
            window,
            cx,
        );
    }
    engine.retention.measured_leaves = leaves;
}

/// Removes a node from the tree, and with it what its measurement captured.
///
/// Taffy keeps a removed node's context until another node is given its slot,
/// so a measurement closure, and everything it holds — the text of a text
/// element and the lines shaped from it — would outlive its node by however
/// many frames that takes. Replacing the closure rather than clearing the
/// context releases all of it without dirtying the node, or a parent it is
/// still attached to, on the way out.
fn remove_node(taffy: &mut TaffyTree<NodeContext>, id: LayoutId) {
    if let Some(context) = taffy.get_node_context_mut(id.into()) {
        let released: Box<MeasureFn> = Box::new(|_, _, _, _| Size::default());
        #[cfg(feature = "stacker")]
        let released = crate::taffy::StackSafe::new(released);
        context.measure = released;
    }
    taffy.remove(id.into()).expect(EXPECT_MESSAGE);
}

/// A node kept from one frame to the next, alongside enough of the request
/// that produced it to tell whether this frame can leave it alone.
///
/// Taffy caches layout results per node and discards that cache for a node and
/// all of its ancestors whenever the node is dirtied. Every mutating call —
/// `set_style`, `set_children`, `set_node_context` — dirties unconditionally,
/// so keeping nodes across frames is worth nothing on its own: the value comes
/// from *not writing* to them, which is only possible by remembering what the
/// previous frame asked for and comparing against it first.
struct RetainedNode {
    id: LayoutId,
    /// Frame in which this node was last claimed. A node that goes a whole
    /// frame unclaimed has left the element tree and is released.
    claimed_in_frame: u64,
    /// The children the node was last given, kept here because reading them
    /// back out of Taffy allocates.
    children: SmallVec<[LayoutId; 8]>,
    /// Whether the node measures its own size.
    measured: bool,
    /// [`layout_fingerprint`] of the style the node was last asked for. While
    /// the request is the same, converting it to a Taffy style and comparing
    /// that against the node's is work with only one possible outcome.
    style_fingerprint: u64,
    /// What the element measuring this node left for the next frame's element
    /// to take its measurement over from. See
    /// [`TaffyLayoutEngine::request_retained_carried_measured_layout`].
    measurement: Option<Rc<dyn Any>>,
    /// The sizes Taffy has taken from the node since it was last dirtied,
    /// for a new measurement to be checked against. See [`MeasureLog`].
    measure_log: Option<Rc<MeasureLog>>,
    /// Whether the node was last claimed laying out a list row, and is kept
    /// for [`LIST_ROW_NODE_LINGER_FRAMES`] after it goes unclaimed. A list
    /// lays out only the rows it shows, or, on a scroll layer, those it
    /// paints ahead of the viewport, so a row scrolled out and back in again
    /// would otherwise be laid out, and its text shaped, from scratch.
    lingers: bool,
}

/// The measurements Taffy took of one measured node since the node was last
/// dirtied: the constraints of each and the size it came out at, in the
/// order they were last taken.
///
/// Taffy caches a node's sizes under the constraints it computed them for,
/// and reuses them until the node is dirtied. Any size it holds for the node
/// was computed from one of these measurements, so a new measurement giving
/// the same size under every one of them would leave all of it — the node's
/// cache and those above it — exactly as it is. Recording stops, and the log
/// can no longer vouch for anything, once it has more entries than a node
/// measured under steady constraints ever needs.
#[derive(Default)]
pub(crate) struct MeasureLog {
    entries: RefCell<SmallVec<[MeasureLogEntry; 4]>>,
    overflowed: Cell<bool>,
}

/// One measurement in a [`MeasureLog`]: the known dimensions and available
/// space it was taken under, and the size it gave.
type MeasureLogEntry = (Size<Option<Pixels>>, Size<AvailableSpace>, Size<Pixels>);

/// How many measurements a [`MeasureLog`] keeps before giving up. Taffy
/// caches ten results per node.
const MEASURE_LOG_CAPACITY: usize = 16;

impl MeasureLog {
    /// Records a measurement Taffy took, moving it to the end if one under the
    /// same constraints was already recorded.
    fn record(
        &self,
        known: Size<Option<Pixels>>,
        available: Size<AvailableSpace>,
        size: Size<Pixels>,
    ) {
        if self.overflowed.get() {
            return;
        }
        let mut entries = self.entries.borrow_mut();
        if let Some(ix) = entries
            .iter()
            .position(|(k, a, _)| *k == known && *a == available)
        {
            entries.remove(ix);
        } else if entries.len() == MEASURE_LOG_CAPACITY {
            self.overflowed.set(true);
            entries.clear();
            return;
        }
        entries.push((known, available, size));
    }

    /// Whether `measure` gives every recorded size under the constraints it
    /// was recorded under. The measurements are taken again in the order they
    /// were last taken, so what `measure` keeps of them is what the last one
    /// left, as it would be had Taffy taken them.
    fn replays(
        &self,
        measure: &mut impl FnMut(
            Size<Option<Pixels>>,
            Size<AvailableSpace>,
            &mut Window,
            &mut App,
        ) -> Size<Pixels>,
        stats: &mut LayoutStats,
        window: &mut Window,
        cx: &mut App,
    ) -> bool {
        if self.overflowed.get() {
            return false;
        }
        let entries = self.entries.borrow();
        !entries.is_empty()
            && entries.iter().all(|(known, available, size)| {
                stats.measure_calls += 1;
                measure(*known, *available, window, cx) == *size
            })
    }

    /// Whether the log holds every size Taffy took from its node since the
    /// node was last dirtied: it has some, and did not give up recording.
    fn vouches(&self) -> bool {
        !self.overflowed.get() && !self.entries.borrow().is_empty()
    }

    /// `measure`, boxed for a node, recording what it measures into `log`.
    fn logged(
        log: &Rc<MeasureLog>,
        mut measure: impl FnMut(
            Size<Option<Pixels>>,
            Size<AvailableSpace>,
            &mut Window,
            &mut App,
        ) -> Size<Pixels>
        + 'static,
    ) -> Box<MeasureFn> {
        let log = log.clone();
        Box::new(move |known, available, window: &mut Window, cx: &mut App| {
            let size = measure(known, available, window, cx);
            log.record(known, available, size);
            size
        })
    }
}

/// What an element made of the measurement the node it lays out was left
/// with. See [`TaffyLayoutEngine::request_retained_carried_measured_layout`].
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum Adopted {
    /// The measurement does not stand for this element.
    No,
    /// The element took the measurement over, and measures the node from now
    /// on: it differs from the element before it in something measuring it
    /// again would use.
    Measurement,
    /// The element took the measurement over, and the node's closure measures
    /// it as its own would: the node is left as it is.
    Node,
}

/// What [`TaffyLayoutEngine::claim`] found for an element's key.
enum Claim {
    /// A node from an earlier frame, now claimed for this one.
    Reused(u64, LayoutId),
    /// No node yet; one should be allocated and retained under this key.
    Vacant(u64),
    /// No node may be retained for this element: either it has no key, or
    /// another element already claimed the one it has.
    Unkeyed,
}

impl TaffyLayoutEngine {
    /// How many nodes the tree is currently holding, retained and transient
    /// alike. Used by tests to check that retention does not leak.
    #[cfg(any(test, feature = "test-support"))]
    pub(crate) fn node_count(&self) -> usize {
        self.taffy.total_node_count()
    }

    /// Takes the node retained under `key` for use in this frame.
    fn claim(&mut self, key: Option<u64>) -> Claim {
        let Some(key) = key else {
            return Claim::Unkeyed;
        };
        let retention = &mut self.retention;
        let frame = retention.frame;
        let Some(node) = retention.retained.get_mut(&key) else {
            return Claim::Vacant(key);
        };
        if node.claimed_in_frame == frame {
            // Two elements resolved to one key. Letting both use the node would
            // corrupt the tree, and retaining the second under the same key
            // would strand the first, so the second goes unkeyed.
            return Claim::Unkeyed;
        }
        node.claimed_in_frame = frame;
        node.lingers = retention.list_rows > 0;
        retention.claimed_this_frame += 1;
        retention.stats.nodes_reused += 1;
        if retention.open_key_recordings > 0 {
            retention.claimed_key_log.push(key);
        }
        Claim::Reused(key, node.id)
    }

    /// Starts recording the keys of the nodes claimed or allocated from now
    /// on, returning where the recording starts. Recordings nest.
    pub(crate) fn record_claimed_keys(&mut self) -> usize {
        self.retention.open_key_recordings += 1;
        self.retention.claimed_key_log.len()
    }

    /// The keys an open recording saw since `start`, where the log was then.
    pub(crate) fn claimed_keys_since(&self, start: usize) -> &[u64] {
        let log = &self.retention.claimed_key_log;
        &log[start.min(log.len())..]
    }

    /// Ends the recording started at `start`, returning the keys it saw.
    pub(crate) fn finish_recording_claimed_keys(&mut self, start: usize) -> Vec<u64> {
        let retention = &mut self.retention;
        let keys = retention.claimed_key_log[start..].to_vec();
        retention.open_key_recordings -= 1;
        if retention.open_key_recordings == 0 {
            retention.claimed_key_log.clear();
        }
        keys
    }

    /// Keeps the nodes retained under `keys` for another frame without
    /// requesting them, for a subtree drawn from what it drew last frame
    /// rather than laid out again: they are there to be reused when it is
    /// built next. Keys already claimed this frame, or no longer retained,
    /// are passed over.
    pub(crate) fn keep_retained(&mut self, keys: &[u64]) {
        let retention = &mut self.retention;
        let frame = retention.frame;
        for key in keys {
            if let Some(node) = retention.retained.get_mut(key)
                && node.claimed_in_frame != frame
            {
                node.claimed_in_frame = frame;
                retention.claimed_this_frame += 1;
                if retention.open_key_recordings > 0 {
                    retention.claimed_key_log.push(*key);
                }
            }
        }
    }

    /// Takes over nodes claimed this frame by a layout request made before
    /// the one being recorded, as if it had claimed them itself: they are
    /// kept, and recorded as its own.
    pub(crate) fn adopt_claimed(&mut self, keys: &[u64]) {
        let retention = &mut self.retention;
        let frame = retention.frame;
        for key in keys {
            let Some(node) = retention.retained.get_mut(key) else {
                continue;
            };
            if node.claimed_in_frame != frame {
                node.claimed_in_frame = frame;
                retention.claimed_this_frame += 1;
            }
            if retention.open_key_recordings > 0 {
                retention.claimed_key_log.push(*key);
            }
        }
    }

    /// Keeps the nodes retained under `keys` for another frame, as
    /// [`Self::keep_retained`] does, but only if every one of them is still
    /// retained and unclaimed this frame; otherwise keeps none of them and
    /// returns false. A subtree whose layout is reused without being requested
    /// again needs all of its nodes, just as they were.
    /// The layouts last computed for the retained nodes `keys`, to tell once
    /// layout is computed again whether it changed them. See
    /// [`Self::layouts_unchanged`].
    pub(crate) fn retained_layouts(&self, keys: &[u64]) -> Vec<(LayoutId, taffy::Layout)> {
        keys.iter()
            .filter_map(|key| self.retention.retained.get(key))
            .filter_map(|node| Some((node.id, *self.taffy.layout(node.id.into()).ok()?)))
            .collect()
    }

    /// Whether computing layout at `root` reaches `id`. Element ancestry
    /// does not imply layout ancestry: list items and other children placed
    /// with `prepaint_as_root` belong to separate Taffy trees.
    pub(crate) fn layout_reaches(&self, root: LayoutId, id: LayoutId) -> bool {
        let root = root.into();
        let mut node = id.into();
        loop {
            if node == root {
                return true;
            }
            let Some(parent) = self.taffy.parent(node) else {
                return false;
            };
            node = parent;
        }
    }

    /// Whether the nodes [`Self::retained_layouts`] returned still have the
    /// layouts they had then.
    pub(crate) fn layouts_unchanged(&self, layouts: &[(LayoutId, taffy::Layout)]) -> bool {
        layouts.iter().all(|(id, layout)| {
            self.taffy
                .layout((*id).into())
                .is_ok_and(|now| now == layout)
        })
    }

    pub(crate) fn try_keep_retained(&mut self, keys: &[u64]) -> bool {
        let frame = self.retention.frame;
        let all_there = keys.iter().all(|key| {
            self.retention
                .retained
                .get(key)
                .is_some_and(|node| node.claimed_in_frame != frame)
        });
        if all_there {
            self.keep_retained(keys);
            self.retention.stats.nodes_reused += keys.len() as u64;
        }
        all_there
    }

    /// Undoes [`Self::try_keep_retained`] for `keys`, so that the subtree can
    /// be laid out again after all this frame and claim its nodes itself.
    pub(crate) fn release_kept(&mut self, keys: &[u64]) {
        let retention = &mut self.retention;
        let frame = retention.frame;
        for key in keys {
            if let Some(node) = retention.retained.get_mut(key)
                && node.claimed_in_frame == frame
            {
                node.claimed_in_frame = frame.wrapping_sub(1);
                retention.claimed_this_frame -= 1;
                retention.stats.nodes_reused = retention.stats.nodes_reused.saturating_sub(1);
            }
        }
    }

    /// How many nodes allocated this frame are not retained. A subtree that
    /// allocated any cannot have its layout reused, since they go at the end
    /// of the frame.
    pub(crate) fn transient_count(&self) -> usize {
        self.retention.transient.len()
    }

    /// See [`LayoutRetention::layout_changes`] on the field.
    pub(crate) fn layout_changes(&self) -> u64 {
        self.retention.layout_changes
    }

    /// See [`LayoutRetention::remeasures`] on the field.
    pub(crate) fn remeasures(&self) -> u64 {
        self.retention.remeasures
    }

    /// Records a freshly allocated node under `key`, or as transient when there
    /// is no key to record it under.
    fn retain(
        &mut self,
        key: Option<u64>,
        id: LayoutId,
        children: &[LayoutId],
        measured: bool,
        style_fingerprint: u64,
    ) {
        let retention = &mut self.retention;
        let Some(key) = key else {
            retention.transient.push(id);
            return;
        };
        retention.retained.insert(
            key,
            RetainedNode {
                id,
                claimed_in_frame: retention.frame,
                children: SmallVec::from_slice(children),
                measured,
                style_fingerprint,
                measurement: None,
                measure_log: None,
                lingers: retention.list_rows > 0,
            },
        );
        retention.claimed_this_frame += 1;
        if retention.open_key_recordings > 0 {
            retention.claimed_key_log.push(key);
        }
    }

    /// Brings a retained node's style up to date with `style`, converting and
    /// comparing it only when it is not the request the node was last given.
    fn apply_requested_style(
        &mut self,
        key: u64,
        id: LayoutId,
        style: &Style,
        rem_size: Pixels,
        scale_factor: f32,
    ) {
        let fingerprint = layout_fingerprint(style, rem_size, scale_factor);
        let node = self
            .retention
            .retained
            .get_mut(&key)
            .expect("a claimed key is always present");
        if node.style_fingerprint == fingerprint {
            self.retention.stats.style_compares += 1;
            // A field `layout_fingerprint` fails to read would leave the node
            // with a stale style whenever only that field changed, and nothing
            // would say so; debug builds compare in full to catch one.
            debug_assert!(
                self.retention
                    .unstretched_styles
                    .get(&id)
                    .unwrap_or_else(|| self.taffy.style(id.into()).expect(EXPECT_MESSAGE))
                    == &style.to_taffy(rem_size, scale_factor),
                "layout_fingerprint matched a style that converts differently; \
                 it has to read every field to_taffy does"
            );
            return;
        }
        node.style_fingerprint = fingerprint;
        self.apply_style(id, style.to_taffy(rem_size, scale_factor));
    }

    /// Writes `style` to a node, but only if it differs from what the node was
    /// last asked for.
    fn apply_style(&mut self, id: LayoutId, style: taffy::style::Style) {
        let retention = &mut self.retention;
        retention.stats.style_compares += 1;
        let previous = retention
            .unstretched_styles
            .get(&id)
            .unwrap_or_else(|| self.taffy.style(id.into()).expect(EXPECT_MESSAGE));
        if previous == &style {
            return;
        }
        // Taffy now holds exactly what was requested, so the stretched-style
        // bookkeeping no longer applies.
        retention.unstretched_styles.remove(&id);
        retention.stats.style_writes += 1;
        retention.layout_changes += 1;
        self.taffy
            .set_style(id.into(), style)
            .expect(EXPECT_MESSAGE);
    }

    /// Writes `children` to a retained node, but only if the list changed.
    fn apply_children(&mut self, key: u64, id: LayoutId, children: &[LayoutId]) {
        let retention = &mut self.retention;
        let node = retention
            .retained
            .get_mut(&key)
            .expect("a claimed key is always present");
        if node.children.as_slice() == children {
            return;
        }
        node.children.clear();
        node.children.extend_from_slice(children);
        retention.stats.children_writes += 1;
        retention.layout_changes += 1;
        self.taffy
            // This is safe because LayoutId is repr(transparent) to taffy::tree::NodeId.
            .set_children(id.into(), LayoutId::to_taffy_slice(children))
            .expect(EXPECT_MESSAGE);
    }

    /// Adds a self-measuring leaf to the layout tree as
    /// [`request_retained_measured_layout`] does, but lets the element
    /// take over the measurement of the node retained under `key`, rather
    /// than have it taken again.
    ///
    /// The element measuring the node last frame left what it measured from,
    /// `state`, there; `adopt` is given this element's and that one, and takes
    /// the measurement over if it still stands, in which case the node is not
    /// dirtied, and keeps what Taffy cached for it and the nodes above it.
    /// When `adopt` finds the node's measurement stands as it is, the node
    /// keeps its closure and state too, and this element's `state` is dropped;
    /// otherwise the node is given this element's, to measure it from and to
    /// leave for the next frame's.
    ///
    /// When the measurement does not stand — a text element whose text
    /// changed — the node is still left clean if `measure` gives every size
    /// Taffy has taken from the node since it was last dirtied, under the
    /// same constraints: what Taffy cached for the node, and for the nodes
    /// above it, is then what measuring it afresh would produce. A price that
    /// ticks in a cell keeps its width, and the row, the list and the window
    /// around it are not laid out again. See [`MeasureLog`].
    ///
    /// Otherwise the node is measured afresh, and `forget` is first given
    /// `state` to drop what those measurements left in it: a measurement
    /// taken under the last element's constraints is not one Taffy asked this
    /// element for, and `measure` may answer later questions from it.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn request_retained_carried_measured_layout<S: 'static>(
        &mut self,
        key: Option<u64>,
        rem_size: Pixels,
        scale_factor: f32,
        state: S,
        adopt: impl FnOnce(&S, &dyn Any) -> Adopted,
        forget: impl FnOnce(&S),
        measure: impl Fn(
            &S,
            Size<Option<Pixels>>,
            Size<AvailableSpace>,
            &mut Window,
            &mut App,
        ) -> Size<Pixels>
        + 'static,
        window: &mut Window,
        cx: &mut App,
    ) -> LayoutId {
        // The leaf is laid out in the default style, whose fingerprint only
        // changes with the rem size and the scale factor.
        let style_fingerprint = match self.retention.default_fingerprint {
            Some((rem, scale, fingerprint)) if rem == rem_size && scale == scale_factor => {
                fingerprint
            }
            _ => {
                let fingerprint = layout_fingerprint(&Style::default(), rem_size, scale_factor);
                self.retention.default_fingerprint = Some((rem_size, scale_factor, fingerprint));
                fingerprint
            }
        };
        let frame = self.retention.frame;
        let reusable = key
            .and_then(|key| self.retention.retained.get(&key))
            .filter(|node| {
                node.claimed_in_frame != frame
                    && node.measured
                    && node.style_fingerprint == style_fingerprint
            })
            .map(|node| (node.measurement.clone(), node.measure_log.clone()));
        if let Some((previous, log)) = reusable {
            let adopted = previous.map_or(Adopted::No, |previous| adopt(&state, &*previous));
            let kept = if adopted != Adopted::No {
                self.retention.stats.measurements_kept += 1;
                true
            } else if let Some(log) = &log
                && log.replays(
                    &mut |known, available, window: &mut Window, cx: &mut App| {
                        measure(&state, known, available, window, cx)
                    },
                    &mut self.retention.stats,
                    window,
                    cx,
                )
            {
                self.retention.stats.measurements_replayed += 1;
                true
            } else {
                false
            };
            if kept
                && let Claim::Reused(key, id) = self.claim(key)
                && let Some(context) = self.taffy.get_node_context_mut(id.into())
            {
                if adopted == Adopted::Node {
                    return id;
                }
                let state = Rc::new(state);
                let memo: Rc<dyn Any> = state.clone();
                let log = log.unwrap_or_default();
                let measure = MeasureLog::logged(&log, move |known, available, window, cx| {
                    measure(&state, known, available, window, cx)
                });
                #[cfg(feature = "stacker")]
                let measure = crate::taffy::StackSafe::new(measure);
                context.measure = measure;
                let node = self
                    .retention
                    .retained
                    .get_mut(&key)
                    .expect("a claimed key is always present");
                node.measurement = Some(memo);
                node.measure_log = Some(log);
                return id;
            }
            forget(&state);
        }

        let state = Rc::new(state);
        let memo: Rc<dyn Any> = state.clone();
        let measure = move |known, available, window: &mut Window, cx: &mut App| {
            measure(&state, known, available, window, cx)
        };
        let log = Rc::<MeasureLog>::default();
        let measure = MeasureLog::logged(&log, measure);
        // Measured afresh: what the last element measured was forgotten, and
        // measuring under its constraints again would put it back.
        let id = request_measured_node(
            self,
            key,
            &Style::default(),
            rem_size,
            scale_factor,
            measure,
            false,
        );
        if let Some(node) = key.and_then(|key| self.retention.retained.get_mut(&key))
            && node.id == id
        {
            node.measurement = Some(memo);
            node.measure_log = Some(log);
        }
        id
    }

    /// Lays out again the subtree under `id`, a node already placed by its
    /// parent this frame, within `available_space`, leaving it where its
    /// parent put it.
    ///
    /// Computing a layout from a node treats it as a root and moves it to the
    /// origin; its absolute position, worked out before, is put back so that
    /// the bounds of everything under it are found relative to it as before.
    ///
    /// A root is sized by its own style, so a node its parent stretched would
    /// shrink to its content. It is held at the size its parent gave it while
    /// it is laid out, and given back the style it asked for afterwards.
    pub(crate) fn relayout_in_place(
        &mut self,
        id: LayoutId,
        available_space: Size<AvailableSpace>,
        window: &mut Window,
        cx: &mut App,
    ) {
        let scale_factor = window.scale_factor();
        let bounds = self.layout_bounds(id, scale_factor);
        let origin = self
            .absolute_outer_origins
            .get(&id)
            .copied()
            .expect("layout_bounds caches the absolute origin");
        let requested = self.taffy.style(id.into()).expect(EXPECT_MESSAGE).clone();
        let given = self.taffy.layout(id.into()).expect(EXPECT_MESSAGE).size;
        let mut held = requested.clone();
        held.size = taffy::geometry::Size {
            width: taffy::style::Dimension::length(given.width),
            height: taffy::style::Dimension::length(given.height),
        };
        held.min_size = held.size;
        held.max_size = held.size;
        held.box_sizing = taffy::style::BoxSizing::BorderBox;
        self.taffy.set_style(id.into(), held).expect(EXPECT_MESSAGE);
        self.compute_layout(id, available_space, window, cx);
        self.taffy
            .set_style(id.into(), requested)
            .expect(EXPECT_MESSAGE);
        let stack = &mut self.layout_bounds_scratch_space;
        stack.push(id);
        while let Some(id) = stack.pop() {
            self.absolute_layout_bounds.remove(&id);
            self.absolute_outer_origins.remove(&id);
            stack.extend(
                self.taffy
                    .children(id.into())
                    .expect(EXPECT_MESSAGE)
                    .into_iter()
                    .map(LayoutId::from),
            );
        }
        self.absolute_outer_origins.insert(id, origin);
        self.absolute_layout_bounds.insert(id, bounds);
    }
}

/// Ends the frame for the retained nodes: releases nodes that no longer
/// appear in the element tree. See [`TaffyLayoutEngine::clear`].
///
/// Nodes that were claimed this frame stay, along with their Taffy layout
/// caches, which is what lets the next frame skip recomputing the parts of
/// the tree that did not change.
#[inline]
pub(crate) fn release_unclaimed_nodes(engine: &mut TaffyLayoutEngine) {
    // A measured node left clean but not laid out this frame is measured
    // afresh the next time it is.
    for (id, _) in mem::take(&mut engine.retention.pending_replays) {
        let _ = engine.taffy.mark_dirty(id.into());
    }
    let retention = &mut engine.retention;
    retention.stats.frames += 1;

    for id in retention.transient.drain(..) {
        retention.unstretched_styles.remove(&id);
        remove_node(&mut engine.taffy, id);
        retention.stats.nodes_freed += 1;
    }

    // In a steady frame every retained node was claimed, and there is
    // nothing to sweep.
    if retention.retained.len() != retention.claimed_this_frame {
        let frame = retention.frame;
        let taffy = &mut engine.taffy;
        let unstretched_styles = &mut retention.unstretched_styles;
        let freed = &mut retention.stats.nodes_freed;
        retention.retained.retain(|_, node| {
            if node.claimed_in_frame == frame
                || node.lingers && frame - node.claimed_in_frame < LIST_ROW_NODE_LINGER_FRAMES
            {
                return true;
            }
            unstretched_styles.remove(&node.id);
            remove_node(taffy, node.id);
            *freed += 1;
            false
        });
    }

    retention.claimed_this_frame = 0;
    retention.frame += 1;
}

/// [`Window::request_layout`] for a style the caller keeps, and children it
/// holds in a slice: neither is copied on the way to the layout engine, where
/// the window's method takes a style of 600-odd bytes by value, and copies it
/// at every call it passes through.
#[inline]
pub(crate) fn request_layout(
    window: &mut Window,
    style: &Style,
    children: &[LayoutId],
) -> LayoutId {
    window.invalidator.debug_assert_prepaint();
    let rem_size = window.rem_size();
    let scale_factor = window.scale_factor();
    let key = crate::fast::layout_key::layout_key(window);
    request_retained_layout(
        window.layout_engine.as_mut().unwrap(),
        key,
        style,
        rem_size,
        scale_factor,
        children,
    )
}

/// Adds a node to the layout tree, reusing the one retained under `key`
/// when there is one. See [`TaffyLayoutEngine::request_layout`].
///
/// `key` identifies this element's position in the element tree across
/// frames; `None` opts out of reuse, and the node is released at the end of
/// the frame.
#[inline]
pub(crate) fn request_retained_layout(
    engine: &mut TaffyLayoutEngine,
    key: Option<u64>,
    style: &Style,
    rem_size: Pixels,
    scale_factor: f32,
    children: &[LayoutId],
) -> LayoutId {
    let key = match engine.claim(key) {
        Claim::Reused(key, id) => {
            engine.apply_requested_style(key, id, style, rem_size, scale_factor);
            engine.apply_children(key, id, children);
            // A node that measured itself on an earlier frame no longer does.
            if engine
                .retention
                .retained
                .get(&key)
                .is_some_and(|node| node.measured)
            {
                let node = engine
                    .retention
                    .retained
                    .get_mut(&key)
                    .expect("a claimed key is always present");
                node.measured = false;
                node.measurement = None;
                node.measure_log = None;
                engine
                    .taffy
                    .set_node_context(id.into(), None)
                    .expect(EXPECT_MESSAGE);
            }
            return id;
        }
        Claim::Vacant(key) => Some(key),
        Claim::Unkeyed => None,
    };

    engine.retention.stats.nodes_created += 1;
    engine.retention.layout_changes += 1;
    let style_fingerprint = layout_fingerprint(style, rem_size, scale_factor);
    let taffy_style = style.to_taffy(rem_size, scale_factor);
    let id: LayoutId = engine
        .taffy
        .new_leaf(taffy_style)
        .expect(EXPECT_MESSAGE)
        .into();
    if !children.is_empty() {
        // A retained child can arrive here still listed under the parent
        // it had last frame. `new_with_children` would leave it listed
        // there, so when that parent's children were next rewritten the
        // child would lose its parent link, and with it the offset
        // `layout_bounds` adds up from its ancestors. `set_children`
        // detaches each child from wherever it was first.
        engine
            .taffy
            // This is safe because LayoutId is repr(transparent) to taffy::tree::NodeId.
            .set_children(id.into(), LayoutId::to_taffy_slice(children))
            .expect(EXPECT_MESSAGE);
    }
    engine.retain(key, id, children, false, style_fingerprint);
    id
}

/// Adds a engine-measuring leaf to the layout tree, reusing the node retained
/// under `key` when there is one. See
/// [`TaffyLayoutEngine::request_measured_layout`].
///
/// Nothing says what the measurement depends on, so a reused node is given
/// the new closure, and `measure` is guaranteed to run: before the node is
/// laid out, under every constraint Taffy measured the node under since it
/// was last dirtied, when its [`MeasureLog`] holds them all, and the node is
/// dirtied only if a size comes out different (see
/// [`replay_pending_measures`]); as Taffy lays it out otherwise.
#[inline]
pub(crate) fn request_retained_measured_layout(
    engine: &mut TaffyLayoutEngine,
    key: Option<u64>,
    style: &Style,
    rem_size: Pixels,
    scale_factor: f32,
    measure: impl FnMut(
        Size<Option<Pixels>>,
        Size<AvailableSpace>,
        &mut Window,
        &mut App,
    ) -> Size<Pixels>
    + 'static,
) -> LayoutId {
    request_measured_node(engine, key, style, rem_size, scale_factor, measure, true)
}

/// [`request_retained_measured_layout`], checking a reused node's new closure
/// against its [`MeasureLog`] only when `replay`.
fn request_measured_node(
    engine: &mut TaffyLayoutEngine,
    key: Option<u64>,
    style: &Style,
    rem_size: Pixels,
    scale_factor: f32,
    measure: impl FnMut(
        Size<Option<Pixels>>,
        Size<AvailableSpace>,
        &mut Window,
        &mut App,
    ) -> Size<Pixels>
    + 'static,
    replay: bool,
) -> LayoutId {
    engine.retention.stats.measure_rebinds += 1;
    // What the node measured since it was last dirtied, if it can vouch for
    // it: the new closure is then checked against it before the node is laid
    // out, rather than the node dirtied now.
    let vouching = match &key {
        Some(key) if replay => engine
            .retention
            .retained
            .get(key)
            .and_then(|node| node.measure_log.clone())
            .filter(|log| log.vouches()),
        _ => None,
    };
    let log = vouching.clone().unwrap_or_default();
    let measure = MeasureLog::logged(&log, measure);
    #[cfg(feature = "stacker")]
    let measure = crate::taffy::StackSafe::new(measure);

    let (key, id) = match engine.claim(key) {
        Claim::Reused(key, id) => (key, id),
        claim => {
            let key = match claim {
                Claim::Vacant(key) => Some(key),
                _ => None,
            };
            let style_fingerprint = layout_fingerprint(style, rem_size, scale_factor);
            let taffy_style = style.to_taffy(rem_size, scale_factor);
            engine.retention.stats.nodes_created += 1;
            engine.retention.layout_changes += 1;
            let id: LayoutId = engine
                .taffy
                .new_leaf_with_context(taffy_style, NodeContext { measure })
                .expect(EXPECT_MESSAGE)
                .into();
            engine.retain(key, id, &[], true, style_fingerprint);
            if let Some(node) = key.and_then(|key| engine.retention.retained.get_mut(&key))
                && node.id == id
            {
                node.measure_log = Some(log);
            }
            return id;
        }
    };

    engine.apply_requested_style(key, id, style, rem_size, scale_factor);
    engine.apply_children(key, id, &[]);

    // Nothing says whether the measurement still stands, and what it
    // produces lives in state the element made afresh this frame, so it
    // has to be taken again: before the node is laid out, against what it
    // measured before, when that is known, or as Taffy lays it out.
    engine.retention.remeasures += 1;
    if let Some(context) = engine.taffy.get_node_context_mut(id.into()) {
        context.measure = measure;
    } else {
        engine
            .taffy
            .set_node_context(id.into(), Some(NodeContext { measure }))
            .expect(EXPECT_MESSAGE);
    }
    match vouching {
        Some(log) => engine.retention.pending_replays.push((id, log)),
        None => engine.taffy.mark_dirty(id.into()).expect(EXPECT_MESSAGE),
    }
    let node = engine
        .retention
        .retained
        .get_mut(&key)
        .expect("a claimed key is always present");
    node.measured = true;
    node.measurement = None;
    node.measure_log = Some(log);

    id
}

/// Measures, with the closures they were given this frame, the measured
/// nodes [`request_retained_measured_layout`] left clean, under every
/// constraint their [`MeasureLog`] holds, dirtying those whose size came out
/// different under any of them. Called before a layout is computed, as the
/// closures need the window to measure.
///
/// A node every one of whose sizes stands keeps what Taffy cached for it and
/// for the nodes above it: a list row whose text is laid out again, as it is
/// on every frame the row is built, is not laid out afresh. The measurements
/// are taken in the order they were last taken, so what the element keeps of
/// them is what the last one left, as it would be had Taffy taken them.
pub(crate) fn replay_pending_measures(
    engine: &mut TaffyLayoutEngine,
    window: &mut Window,
    cx: &mut App,
) {
    if engine.retention.pending_replays.is_empty() {
        return;
    }
    let pending = mem::take(&mut engine.retention.pending_replays);
    for (id, log) in &pending {
        let entries = log.entries.borrow().clone();
        let Some(context) = engine.taffy.get_node_context_mut((*id).into()) else {
            continue;
        };
        let stands = entries.iter().all(|(known, available, size)| {
            engine.retention.stats.measure_calls += 1;
            (context.measure)(*known, *available, window, cx) == *size
        });
        if stands {
            engine.retention.stats.measurements_replayed += 1;
            // What the element keeps is what the last measurement left; the
            // node is measured again at the width it is laid out at, should
            // that be another, once the layout is computed.
            if let Some((known, available, _)) = entries.last() {
                engine
                    .retention
                    .measured_leaves
                    .note((*id).into(), *known, *available);
            }
        } else {
            engine.retention.layout_changes += 1;
            engine.taffy.mark_dirty((*id).into()).expect(EXPECT_MESSAGE);
        }
    }
    engine.retention.pending_replays = pending;
    engine.retention.pending_replays.clear();
}

/// Runs `f`, laying out a list row: the nodes it claims linger. See
/// [`RetainedNode::lingers`].
pub(crate) fn laying_out_list_row<R>(window: &mut Window, f: impl FnOnce(&mut Window) -> R) -> R {
    window.layout_engine.as_mut().unwrap().retention.list_rows += 1;
    let result = f(window);
    window.layout_engine.as_mut().unwrap().retention.list_rows -= 1;
    result
}

/// Treats any `auto` dimension of the given node's style as filling `size`.
/// See [`TaffyLayoutEngine::stretch_auto_size_to_fill`].
///
/// The style Taffy ends up holding is not the one the element asked for, and
/// the difference is not recoverable from the result — a stretched `auto`
/// looks exactly like an explicit length. The requested style is therefore
/// kept aside so the next frame compares like with like instead of
/// rewriting, and dirtying, the root on every frame.
#[inline]
pub(crate) fn stretch_retained_auto_size_to_fill(
    engine: &mut TaffyLayoutEngine,
    id: LayoutId,
    size: Size<Pixels>,
    scale_factor: f32,
) {
    let retention = &mut engine.retention;
    let requested = match retention.unstretched_styles.get(&id) {
        Some(requested) => requested,
        None => engine.taffy.style(id.into()).expect(EXPECT_MESSAGE),
    };
    let stretch_width = requested.size.width.is_auto();
    let stretch_height = requested.size.height.is_auto();
    if !stretch_width && !stretch_height {
        return;
    }

    let requested = requested.clone();
    let mut style = requested.clone();
    if stretch_width {
        style.size.width =
            taffy::style::Dimension::length(round_to_device_pixel(size.width.0, scale_factor));
    }
    if stretch_height {
        style.size.height =
            taffy::style::Dimension::length(round_to_device_pixel(size.height.0, scale_factor));
    }
    if engine.taffy.style(id.into()).expect(EXPECT_MESSAGE) != &style {
        retention.stats.style_writes += 1;
        engine
            .taffy
            .set_style(id.into(), style)
            .expect(EXPECT_MESSAGE);
    }
    retention.unstretched_styles.insert(id, requested);
}

/// A hash of everything in `style` that its conversion to a Taffy style reads,
/// and of what that conversion resolves lengths against.
///
/// It has to read exactly the fields [`ToTaffy`](crate::taffy::ToTaffy) does:
/// a field it misses leaves a retained node with a stale style whenever only
/// that field changes. Debug builds check every match against a full conversion.
fn layout_fingerprint(style: &Style, rem_size: Pixels, scale_factor: f32) -> u64 {
    fn absolute(hasher: &mut FxHasher, length: &AbsoluteLength) {
        match length {
            AbsoluteLength::Pixels(pixels) => (0u8, pixels.0.to_bits()).hash(hasher),
            AbsoluteLength::Rems(rems) => (1u8, rems.0.to_bits()).hash(hasher),
        }
    }
    fn definite(hasher: &mut FxHasher, length: &DefiniteLength) {
        match length {
            DefiniteLength::Absolute(length) => {
                0u8.hash(hasher);
                absolute(hasher, length);
            }
            DefiniteLength::Fraction(fraction) => (1u8, fraction.to_bits()).hash(hasher),
        }
    }
    fn length(hasher: &mut FxHasher, length: &Length) {
        match length {
            Length::Definite(length) => {
                0u8.hash(hasher);
                definite(hasher, length);
            }
            Length::Auto => 1u8.hash(hasher),
        }
    }
    fn edges<T: Clone + Debug + Default + PartialEq>(
        hasher: &mut FxHasher,
        edges: &Edges<T>,
        each: fn(&mut FxHasher, &T),
    ) {
        each(hasher, &edges.top);
        each(hasher, &edges.right);
        each(hasher, &edges.bottom);
        each(hasher, &edges.left);
    }
    fn sizes<T: Clone + Debug + Default + PartialEq>(
        hasher: &mut FxHasher,
        size: &Size<T>,
        each: fn(&mut FxHasher, &T),
    ) {
        each(hasher, &size.width);
        each(hasher, &size.height);
    }
    fn placement(hasher: &mut FxHasher, placement: &crate::GridPlacement) {
        match placement {
            crate::GridPlacement::Line(line) => (0u8, *line).hash(hasher),
            crate::GridPlacement::Span(span) => (1u8, *span).hash(hasher),
            crate::GridPlacement::Auto => 2u8.hash(hasher),
        }
    }
    fn template(hasher: &mut FxHasher, template: &Option<GridTemplate>) {
        match template {
            Some(template) => {
                (1u8, template.repeat).hash(hasher);
                mem::discriminant(&template.min_size).hash(hasher);
            }
            None => 0u8.hash(hasher),
        }
    }

    let mut hasher = FxHasher::default();
    let hasher = &mut hasher;
    rem_size.0.to_bits().hash(hasher);
    scale_factor.to_bits().hash(hasher);

    mem::discriminant(&style.display).hash(hasher);
    mem::discriminant(&style.overflow.x).hash(hasher);
    mem::discriminant(&style.overflow.y).hash(hasher);
    absolute(hasher, &style.scrollbar_width);
    mem::discriminant(&style.position).hash(hasher);
    edges(hasher, &style.inset, length);
    sizes(hasher, &style.size, length);
    sizes(hasher, &style.min_size, length);
    sizes(hasher, &style.max_size, length);
    style.aspect_ratio.map(f32::to_bits).hash(hasher);
    edges(hasher, &style.margin, length);
    edges(hasher, &style.padding, definite);
    edges(hasher, &style.border_widths, absolute);
    style
        .align_items
        .map(|x| mem::discriminant(&x))
        .hash(hasher);
    style.align_self.map(|x| mem::discriminant(&x)).hash(hasher);
    style
        .align_content
        .map(|x| mem::discriminant(&x))
        .hash(hasher);
    style
        .justify_content
        .map(|x| mem::discriminant(&x))
        .hash(hasher);
    sizes(hasher, &style.gap, definite);
    mem::discriminant(&style.flex_direction).hash(hasher);
    mem::discriminant(&style.flex_wrap).hash(hasher);
    length(hasher, &style.flex_basis);
    style.flex_grow.to_bits().hash(hasher);
    style.flex_shrink.to_bits().hash(hasher);
    template(hasher, &style.grid_rows);
    template(hasher, &style.grid_cols);
    match &style.grid_location {
        Some(location) => {
            1u8.hash(hasher);
            for line in [&location.row, &location.column] {
                placement(hasher, &line.start);
                placement(hasher, &line.end);
            }
        }
        None => 0u8.hash(hasher),
    }
    hasher.finish()
}

#[cfg(test)]
mod tests {
    use super::layout_fingerprint;
    use crate::px;
    use crate::{GridTemplate, Style, taffy::ToTaffy as _};

    /// Every field the conversion to a Taffy style reads has to reach the
    /// fingerprint too: a change the fingerprint cannot see is one a retained
    /// node never receives. Each case changes one field that the conversion
    /// reads, and both have to notice.
    #[test]
    fn the_layout_fingerprint_sees_every_field_the_taffy_style_is_made_from() {
        use crate::{
            AlignContent, AlignItems, Display, FlexDirection, FlexWrap, GridLocation,
            GridPlacement, GridTemplateMinSize, Overflow, Position, relative, rems,
        };

        let (rem_size, scale_factor) = (px(16.), 2.);
        let base = Style::default();
        let cases: Vec<(&str, Box<dyn Fn(&mut Style)>)> = vec![
            ("display", Box::new(|s| s.display = Display::Grid)),
            ("overflow.x", Box::new(|s| s.overflow.x = Overflow::Hidden)),
            ("overflow.y", Box::new(|s| s.overflow.y = Overflow::Scroll)),
            (
                "scrollbar_width",
                Box::new(|s| s.scrollbar_width = px(7.).into()),
            ),
            ("position", Box::new(|s| s.position = Position::Absolute)),
            ("inset", Box::new(|s| s.inset.left = px(3.).into())),
            ("size", Box::new(|s| s.size.width = px(40.).into())),
            ("size in rems", Box::new(|s| s.size.width = rems(2.).into())),
            (
                "size as a fraction",
                Box::new(|s| s.size.width = relative(0.5).into()),
            ),
            ("min_size", Box::new(|s| s.min_size.height = px(5.).into())),
            ("max_size", Box::new(|s| s.max_size.width = px(90.).into())),
            ("aspect_ratio", Box::new(|s| s.aspect_ratio = Some(1.5))),
            ("margin", Box::new(|s| s.margin.top = px(2.).into())),
            ("padding", Box::new(|s| s.padding.bottom = px(4.).into())),
            (
                "border_widths",
                Box::new(|s| s.border_widths.right = px(1.).into()),
            ),
            (
                "align_items",
                Box::new(|s| s.align_items = Some(AlignItems::Center)),
            ),
            (
                "align_self",
                Box::new(|s| s.align_self = Some(AlignItems::End)),
            ),
            (
                "align_content",
                Box::new(|s| s.align_content = Some(AlignContent::End)),
            ),
            (
                "justify_content",
                Box::new(|s| s.justify_content = Some(AlignContent::Center)),
            ),
            ("gap", Box::new(|s| s.gap.width = px(6.).into())),
            (
                "flex_direction",
                Box::new(|s| s.flex_direction = FlexDirection::Column),
            ),
            ("flex_wrap", Box::new(|s| s.flex_wrap = FlexWrap::Wrap)),
            ("flex_basis", Box::new(|s| s.flex_basis = px(12.).into())),
            ("flex_grow", Box::new(|s| s.flex_grow = 1.)),
            ("flex_shrink", Box::new(|s| s.flex_shrink = 0.)),
            (
                "grid_rows",
                Box::new(|s| {
                    s.grid_rows = Some(GridTemplate {
                        repeat: 3,
                        min_size: GridTemplateMinSize::Zero,
                    })
                }),
            ),
            (
                "grid_cols",
                Box::new(|s| {
                    s.grid_cols = Some(GridTemplate {
                        repeat: 2,
                        min_size: GridTemplateMinSize::MinContent,
                    })
                }),
            ),
            (
                "grid_location",
                Box::new(|s| {
                    s.grid_location = Some(GridLocation {
                        row: GridPlacement::Line(1)..GridPlacement::Span(2),
                        column: GridPlacement::Auto..GridPlacement::Auto,
                    })
                }),
            ),
        ];

        let base_fingerprint = layout_fingerprint(&base, rem_size, scale_factor);
        let base_taffy = base.to_taffy(rem_size, scale_factor);
        for (field, change) in &cases {
            let mut style = base.clone();
            change(&mut style);
            assert_ne!(
                style.to_taffy(rem_size, scale_factor),
                base_taffy,
                "changing {field} should change the Taffy style, or this case tests nothing"
            );
            assert_ne!(
                layout_fingerprint(&style, rem_size, scale_factor),
                base_fingerprint,
                "changing {field} changes the Taffy style but not the fingerprint"
            );
        }

        // Lengths are resolved against these, so they are inputs as much as
        // the style is.
        let mut in_rems = base;
        in_rems.size.width = rems(2.).into();
        assert_ne!(
            layout_fingerprint(&in_rems, rem_size, scale_factor),
            layout_fingerprint(&in_rems, px(20.), scale_factor)
        );
        assert_ne!(
            layout_fingerprint(&in_rems, rem_size, scale_factor),
            layout_fingerprint(&in_rems, rem_size, 1.)
        );
    }
}