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
// The items a store logged in its latest updates, read by update number: a
// reader keeps the update it read last and reads the items of the updates
// after it. A reader further behind than the log keeps finds them lost.
use cranpose_core::{SceneUpdate, collections::growth_slack::GrowthSlack};
#[derive(Clone, Debug)]
pub(super) struct UpdateLog<T> {
items: Vec<T>,
// Each kept update, oldest first, with where its items start: `None`
// for one that logged more items than an update keeps.
updates: Vec<(SceneUpdate, Option<usize>)>,
}
impl<T> Default for UpdateLog<T> {
fn default() -> Self {
Self {
items: Vec::new(),
updates: Vec::new(),
}
}
}
// How many updates a log keeps: once it holds `most`, it forgets all but
// the latest `kept`, so an update seldom pays for the forgetting.
#[derive(Clone, Copy)]
pub(super) struct UpdatesKept {
pub(super) most: usize,
pub(super) kept: usize,
}
impl<T> UpdateLog<T> {
// Starts the items of `update`, forgetting the oldest updates as
// `updates` says and those whose items take the log past `most_items`.
#[inline]
pub(super) fn begin(&mut self, update: SceneUpdate, updates: UpdatesKept, most_items: usize) {
if self.updates.len() >= updates.most || self.items.len() - self.start(0) > most_items {
self.forget_oldest(updates.kept, most_items);
}
// The log holds `most` updates at most: it takes their room once.
if self.updates.capacity() < updates.most {
self.updates
.reserve_exact(updates.most - self.updates.len());
}
self.updates.push((update, Some(self.items.len())));
}
// Forgets all but the latest `kept` updates, and more of them while
// their items take the log past `most_items`. Out of line: a log forgets
// once in many updates.
#[inline(never)]
fn forget_oldest(&mut self, kept: usize, most_items: usize) {
let mut gone = self.updates.len().saturating_sub(kept);
while gone < self.updates.len() && self.items.len() - self.start(gone) > most_items {
gone += 1;
}
if gone > 0 {
self.forget(gone);
}
}
// Where the items of the kept update at `index` and those after it
// start.
fn start(&self, index: usize) -> usize {
self.updates[index..]
.iter()
.find_map(|&(_, start)| start)
.unwrap_or(self.items.len())
}
// Forgets the oldest `gone` updates. The items keep their room: the
// updates after fill it again, and the trim at rest gives it back.
fn forget(&mut self, gone: usize) {
let cut = if gone < self.updates.len() {
self.start(gone)
} else {
self.items.len()
};
self.items.drain(..cut);
self.updates.drain(..gone);
for (_, start) in &mut self.updates {
if let Some(start) = start {
*start -= cut;
}
}
}
// Logs `item` in the update in progress, unless the update logged
// `most_in_update` items already: its items are then lost.
#[inline]
pub(super) fn push(&mut self, item: T, most_in_update: usize) {
let Some((_, Some(start))) = self.updates.last().copied() else {
return;
};
if self.items.len() - start < most_in_update {
self.items.push(item);
} else {
self.lose_last(start);
}
}
#[cold]
fn lose_last(&mut self, start: usize) {
self.items.truncate(start);
if let Some(last) = self.updates.last_mut() {
last.1 = None;
}
}
pub(super) fn clear(&mut self) {
self.items.clear();
self.updates.clear();
}
// Gives back at rest the items of the kept updates: a reader that read
// the latest update reads nothing more, and one further behind finds
// them lost.
pub(super) fn trim(&mut self) {
self.items = Vec::new();
for (_, start) in &mut self.updates {
*start = None;
}
self.updates.trim_growth_slack();
}
pub(super) fn heap_bytes(&self) -> usize {
self.items.capacity() * size_of::<T>()
+ self.updates.capacity() * size_of::<(SceneUpdate, Option<usize>)>()
}
// The items of the updates after `read`, when the store's update is
// `now`; `None` when the log lost some of them.
pub(super) fn since(&self, read: SceneUpdate, now: SceneUpdate) -> Option<&[T]> {
if read == now {
return Some(&[]);
}
// A reader mostly lags by one update, the last kept.
let after = read.next();
let first = self
.updates
.iter()
.rposition(|&(update, _)| update == after)?;
let mut kept = self.updates[first..].iter().map(|&(_, start)| start);
let start = kept.next().flatten()?;
kept.all(|start| start.is_some())
.then(|| &self.items[start..])
}
}