1use nativeview_abi::table::effect;
5
6use crate::changes::ChangeSet;
7use crate::id::{IndexVec, NodeId, NodeIndex, TableSet};
8use crate::store::{
9 NodeRow, NodeStore, CHILDREN_DIRTY, CREATED, DEAD_IN_FREE, DEAD_MASK, DEAD_PENDING_FREE,
10 DEAD_RETIRED, INLINE, LIVE,
11};
12use crate::table::TableSlot;
13use crate::tx::Transaction;
14
15#[inline(always)]
19pub(crate) fn row_establishes_inline_context(
20 row: &NodeRow,
21 inline_slots: TableSet,
22 non_inline_slots: TableSet,
23) -> bool {
24 (row.tables.0 & inline_slots.0) != 0 && (row.tables.0 & non_inline_slots.0) == 0
25}
26
27#[inline]
28fn has_inline_ancestor(
29 nodes: &IndexVec<NodeIndex, NodeRow>,
30 inline_slots: TableSet,
31 non_inline_slots: TableSet,
32 index: NodeIndex,
33) -> bool {
34 let mut cur = nodes[index].parent;
35 let limit = nodes.len();
36 let mut hops = 0usize;
37 while !cur.is_null() && hops < limit {
38 let p_idx = cur.node_index();
39 let Some(prow) = nodes.get(p_idx) else {
40 break;
41 };
42 if (prow.flags & LIVE) == 0 || prow.generation != cur.generation() {
43 break;
44 }
45 if row_establishes_inline_context(prow, inline_slots, non_inline_slots) {
46 return true;
47 }
48 cur = prow.parent;
49 hops += 1;
50 }
51 false
52}
53
54impl Transaction<'_> {
55 pub fn commit_into(self, changes: &mut ChangeSet) {
59 let s = self.core.store;
60 changes.clear();
61 s.frame += 1;
62 s.commit_epoch = s.commit_epoch.wrapping_add(1).max(1);
63 changes.frame = s.frame;
64
65 for &id in &s.created {
66 let row = &mut s.nodes[id.node_index()];
67 if row.flags & LIVE != 0 && row.generation == id.generation() {
68 row.flags &= !CREATED;
69 changes.created.push(id);
70 }
71 }
72 s.created.clear();
73 changes.removed.append(&mut s.removed);
74
75 for &index in s.reparented.as_slice() {
76 let row = &mut s.nodes[index];
77 if row.flags & LIVE != 0 {
78 row.flags &= !CHILDREN_DIRTY;
79 changes
80 .children
81 .push(NodeId::from_index(index, row.generation));
82 }
83 }
84 s.reparented.clear();
85
86 for &index in s.touched.as_slice() {
87 let row = &mut s.nodes[index];
88 if row.flags & LIVE != 0 && row.effects != 0 {
89 if row.effects & effect::REALIZE != 0 {
90 row.primary_slot = s.realization.primary_slot(row.tables);
91 }
92 changes
93 .touched
94 .push(NodeId::from_index(index, row.generation));
95 changes.effects.push(row.effects);
96 }
97 row.effects = 0;
98 }
99 s.touched.clear();
100
101 for (slot, col) in s.columns.iter_mut().enumerate() {
102 let Some(col) = col else { continue };
103 let slot = TableSlot(slot as u8);
104 if col.dirty.as_slice().is_empty() && col.detached.is_empty() {
105 continue;
106 }
107 let t = changes.next_table(slot);
108 let dirty_len = col.dirty.as_slice().len();
109 if t.nodes.capacity() < dirty_len {
110 t.nodes.reserve(dirty_len);
111 }
112 if t.masks.capacity() < dirty_len {
113 t.masks.reserve(dirty_len);
114 }
115 for &index in col.dirty.as_slice() {
116 let node = &s.nodes[index];
117 let attached = node.flags & LIVE != 0 && node.tables.contains(slot);
118 let Some(row) = col.find_row(index) else {
119 continue;
120 };
121 let m = col.masks[row];
122 col.masks[row] = 0;
123 if attached && m != 0 {
124 t.nodes.push(NodeId::from_index(index, node.generation));
125 t.masks.push(m);
126 }
127 }
128 col.dirty.clear();
129 if col.detached.len() > 1 {
130 col.detached.sort_unstable_by_key(|id| id.raw());
131 col.detached.dedup();
132 }
133 for id in col.detached.drain(..) {
134 let node = &s.nodes[id.node_index()];
135 if node.flags & LIVE != 0
136 && node.generation == id.generation()
137 && !node.tables.contains(slot)
138 {
139 t.detached.push(id);
140 }
141 }
142 changes.pop_empty_table();
143 }
144
145 s.sync_inline_flags(changes);
146
147 let res = &mut s.resources;
148 changes
149 .resources_defined
150 .extend(res.defined.iter().copied().filter(|&id| res.is_live(id)));
151 changes
152 .resources_released
153 .extend(res.released.iter().copied().filter(|&id| !res.is_live(id)));
154 res.end_frame();
155
156 for index in s.pending_free.drain(..) {
157 let row = &mut s.nodes[index];
158 if row.flags & LIVE == 0 && (row.flags & DEAD_MASK) == DEAD_PENDING_FREE {
159 if row.generation == u8::MAX {
160 row.flags = DEAD_RETIRED;
162 } else {
163 row.generation += 1;
164 row.flags = DEAD_IN_FREE;
165 s.free.push_back(index);
166 }
167 }
168 }
169 }
170}
171
172impl NodeStore {
173 fn drain_inline_stack(&mut self) {
176 let inline_slots = self.inline_context_slots;
177 let non_inline_slots = self.non_inline_context_slots;
178 while let Some((parent_idx, child_want)) = self.inline_stack.pop() {
179 let span = self.nodes[parent_idx].children;
180 let len = span.len as usize;
181 let off = span.off as usize;
182 for k in 0..len {
183 let cid = self.children.data()[off + k];
184 let cidx = cid.node_index();
185 let crow = &mut self.nodes[cidx];
186 if (crow.flags & LIVE) == 0 || crow.generation != cid.generation() {
187 continue;
188 }
189 let was_inline = (crow.flags & INLINE) != 0;
190 if was_inline != child_want {
191 if child_want {
192 crow.flags |= INLINE;
193 } else {
194 crow.flags &= !INLINE;
195 }
196 if crow.children.len > 0 {
197 let next_want = child_want
198 || row_establishes_inline_context(crow, inline_slots, non_inline_slots);
199 self.inline_stack.push((cidx, next_want));
200 }
201 }
202 }
203 }
204 }
205
206 pub(crate) fn sync_inline_flags(&mut self, changes: &ChangeSet) {
209 let inline_slots = self.inline_context_slots;
210 let non_inline_slots = self.non_inline_context_slots;
211 if inline_slots.is_empty() {
212 return;
213 }
214 self.inline_stack.clear();
215
216 for &id in &changes.created {
218 let idx = id.node_index();
219 let Some(row) = self.nodes.get(idx) else {
220 continue;
221 };
222 if (row.flags & LIVE) == 0 || row.generation != id.generation() {
223 continue;
224 }
225 let want = has_inline_ancestor(&self.nodes, inline_slots, non_inline_slots, idx);
226 let row = &mut self.nodes[idx];
227 if ((row.flags & INLINE) != 0) != want {
228 if want {
229 row.flags |= INLINE;
230 } else {
231 row.flags &= !INLINE;
232 }
233 if row.children.len > 0 {
234 let child_want = want
235 || row_establishes_inline_context(row, inline_slots, non_inline_slots);
236 self.inline_stack.push((idx, child_want));
237 }
238 }
239 }
240 self.drain_inline_stack();
241
242 for &parent in &changes.children {
244 let pidx = parent.node_index();
245 let Some(prow) = self.nodes.get(pidx) else {
246 continue;
247 };
248 if (prow.flags & LIVE) == 0 || prow.generation != parent.generation() {
249 continue;
250 }
251 let self_inline =
252 has_inline_ancestor(&self.nodes, inline_slots, non_inline_slots, pidx);
253 let prow = &mut self.nodes[pidx];
254 if self_inline {
255 prow.flags |= INLINE;
256 } else {
257 prow.flags &= !INLINE;
258 }
259 if prow.children.len > 0 {
260 let child_want = self_inline
261 || row_establishes_inline_context(prow, inline_slots, non_inline_slots);
262 self.inline_stack.push((pidx, child_want));
263 }
264 }
265 self.drain_inline_stack();
266
267 for &id in &changes.touched {
270 let idx = id.node_index();
271 let Some(row) = self.nodes.get(idx) else {
272 continue;
273 };
274 if (row.flags & LIVE) == 0 || row.generation != id.generation() {
275 continue;
276 }
277 if row.parent.is_null() && (row.flags & INLINE) != 0 {
278 let row = &mut self.nodes[idx];
279 row.flags &= !INLINE;
280 if row.children.len > 0 {
281 let child_want =
282 row_establishes_inline_context(row, inline_slots, non_inline_slots);
283 self.inline_stack.push((idx, child_want));
284 }
285 } else if row.children.len > 0 {
286 let child_want = (row.flags & INLINE) != 0
287 || row_establishes_inline_context(row, inline_slots, non_inline_slots);
288 let first_cid = self.children.data()[row.children.off as usize];
289 if let Some(first_row) = self.nodes.get(first_cid.node_index())
290 && (first_row.flags & LIVE) != 0
291 && ((first_row.flags & INLINE) != 0) != child_want
292 {
293 self.inline_stack.push((idx, child_want));
294 }
295 }
296 }
297 self.drain_inline_stack();
298 }
299
300 pub(crate) fn recompute_all_inline_flags(&mut self) {
302 let inline_slots = self.inline_context_slots;
303 let non_inline_slots = self.non_inline_context_slots;
304 let n = self.nodes.len();
305 for i in 1..n {
306 let idx = NodeIndex(i as u32);
307 if (self.nodes[idx].flags & LIVE) == 0 {
308 continue;
309 }
310 let want = !inline_slots.is_empty()
311 && has_inline_ancestor(&self.nodes, inline_slots, non_inline_slots, idx);
312 if want {
313 self.nodes[idx].flags |= INLINE;
314 } else {
315 self.nodes[idx].flags &= !INLINE;
316 }
317 }
318 }
319}
320
321#[inline]
323fn dedup_sorted_node_ids(slice: &mut [NodeId]) -> usize {
324 if slice.len() <= 1 {
325 return slice.len();
326 }
327 let mut write = 1usize;
328 for read in 1..slice.len() {
329 if slice[read] != slice[write - 1] {
330 slice[write] = slice[read];
331 write += 1;
332 }
333 }
334 write
335}
336
337impl crate::store::NodeStore {
338 pub fn publish_table_views(
352 &self,
353 changes: &ChangeSet,
354 out: &mut Vec<nativeview_abi::table::NvTableView>,
355 owners_buf: &mut Vec<NodeId>,
356 ) {
357 out.clear();
358 owners_buf.clear();
359 let mut ranges =
360 [(TableSlot(0), 0usize, 0usize, 0usize, 0usize); nativeview_abi::table::MAX_TABLE_SLOTS];
361 let mut range_len = 0usize;
362 for slot in self.table_slots() {
363 let Some(col) = self.columns[slot.0 as usize].as_ref() else {
364 continue;
365 };
366 if col.desc.flags() & nativeview_abi::table::table_flags::PUBLISH_TO_SCENE == 0 {
367 continue;
368 }
369 let dirty_start = owners_buf.len();
370 if let Some(tc) = changes.table(slot) {
371 for &nid in &tc.nodes {
372 if self.is_live(nid)
373 && self.has_slot(nid, slot)
374 && col.find_row(nid.node_index()).is_some()
375 {
376 owners_buf.push(nid);
377 }
378 }
379 }
380 if owners_buf.len() - dirty_start > 1 {
381 let slice = &mut owners_buf[dirty_start..];
382 slice.sort_unstable_by_key(|id| id.raw());
383 let unique = dedup_sorted_node_ids(slice);
384 owners_buf.truncate(dirty_start + unique);
385 }
386 let dirty_len = owners_buf.len() - dirty_start;
387
388 let detached_start = owners_buf.len();
389 if let Some(tc) = changes.table(slot) {
390 for &nid in &tc.detached {
391 if self.is_live(nid) && !self.has_slot(nid, slot) {
392 owners_buf.push(nid);
393 }
394 }
395 }
396 if owners_buf.len() - detached_start > 1 {
397 let slice = &mut owners_buf[detached_start..];
398 slice.sort_unstable_by_key(|id| id.raw());
399 let unique = dedup_sorted_node_ids(slice);
400 owners_buf.truncate(detached_start + unique);
401 }
402 let detached_len = owners_buf.len() - detached_start;
403
404 ranges[range_len] = (slot, dirty_start, dirty_len, detached_start, detached_len);
405 range_len += 1;
406 }
407
408 for &(slot, dirty_start, dirty_count, detached_start, detached_count) in
409 &ranges[..range_len]
410 {
411 let (Some(col), Some(t)) = (
414 self.columns[slot.0 as usize].as_ref(),
415 self.table_storage(slot),
416 ) else {
417 continue;
418 };
419 out.push(nativeview_abi::table::NvTableView {
420 key_lo: col.desc.key().lo(),
421 key_hi: col.desc.key().hi(),
422 slot: slot.0 as u32,
423 words: col.w as u32,
424 row_count: t.row_count as u32,
425 dirty_count: dirty_count as u32,
426 detached_count: detached_count as u32,
427 row_index_dir_len: t.row_index_dir.len() as u32,
428 row_index_page_count: (t.row_index_pages.len()
429 / nativeview_abi::table::NV_ROW_INDEX_PAGE_LEN as usize)
430 as u32,
431 _pad0: 0,
432 rows: nativeview_abi::slab_ptr(t.rows),
433 owners: nativeview_abi::slab_ptr(t.owners),
434 dirty_owners: nativeview_abi::slab_ptr(&owners_buf[dirty_start..dirty_start + dirty_count]),
435 detached_owners: nativeview_abi::slab_ptr(
436 &owners_buf[detached_start..detached_start + detached_count],
437 ),
438 row_index_dir: nativeview_abi::slab_ptr(t.row_index_dir),
439 row_index_pages: nativeview_abi::slab_ptr(t.row_index_pages),
440 });
441 }
442 }
443}