core: writer-owned relocation

The worker no longer copies rows when an array cannot grow in place. It
reserves a zeroed region, returns the staged descriptor with a commit
flag, and queues every other request until the main thread (the only
writer) has copied the old bytes and sent commit-rows. Writes that raced
the old worker-side copy were lost; lights in the glTF viewer regularly
ended up at the origin because of it.

Amp-Thread-ID: https://ampcode.com/threads/T-01a0b2dd-db32-712d-99ce-12ce4a86d979
Co-authored-by: Heaust Azure <heaust.azure@gmail.com>
This commit is contained in:
Amp
2026-09-18 06:40:44 +00:00
co-authored by heaust
parent 31d3ae21ff
commit 92638e6c9f
8 changed files with 278 additions and 96 deletions
+68 -19
View File
@@ -40,22 +40,23 @@ fn spans_reuse_coalesced_gaps_before_growing() {
let mut data = RenderData::new(4096).unwrap();
data.create_rows("v".into(), 8, 16, "f32".into()).unwrap();
assert_eq!(data.allocate_object("v", 2).unwrap(), (0, false));
assert_eq!(data.allocate_object("v", 3).unwrap(), (2, false));
assert_eq!(data.allocate_object("v", 3).unwrap(), (5, false));
assert_eq!(data.allocate_object("v", 2).unwrap(), (0, false, false));
assert_eq!(data.allocate_object("v", 3).unwrap(), (2, false, false));
assert_eq!(data.allocate_object("v", 3).unwrap(), (5, false, false));
assert_eq!(rows_of(&data, "v"), 8);
// Freeing the two middle spans leaves one coalesced hole of 6 rows.
data.delete_object("v", 2).unwrap();
data.delete_object("v", 5).unwrap();
assert_eq!(data.delete_object("v", 5), Err("OBJECT_UNKNOWN"));
assert_eq!(data.allocate_object("v", 6).unwrap(), (2, false));
assert_eq!(data.allocate_object("v", 6).unwrap(), (2, false, false));
assert_eq!(rows_of(&data, "v"), 8);
// A span that does not fit doubles capacity instead of growing to the exact need.
assert_eq!(data.allocate_object("v", 1).unwrap(), (8, true));
// A span that does not fit doubles capacity instead of growing to the exact need; the only
// array in the arena extends in place, so nothing is staged.
assert_eq!(data.allocate_object("v", 1).unwrap(), (8, true, false));
assert_eq!(rows_of(&data, "v"), 16);
assert_eq!(data.allocate_object("v", 40).unwrap(), (9, true));
assert_eq!(data.allocate_object("v", 40).unwrap(), (9, true, false));
assert_eq!(rows_of(&data, "v"), 56);
assert_eq!(data.allocate_object("v", 0), Err("ROWS"));
}
@@ -64,8 +65,8 @@ fn spans_reuse_coalesced_gaps_before_growing() {
fn deleting_a_span_zeroes_every_row_and_keeps_neighbours() {
let mut data = RenderData::new(4096).unwrap();
data.create_rows("v".into(), 4, 16, "u32".into()).unwrap();
let (first, _) = data.allocate_object("v", 2).unwrap();
let (second, _) = data.allocate_object("v", 1).unwrap();
let (first, ..) = data.allocate_object("v", 2).unwrap();
let (second, ..) = data.allocate_object("v", 1).unwrap();
let base = (data.rows("v").unwrap().offset - data.base) as usize + data.arena_start;
data.arena[base..base + 48].fill(0xAB);
@@ -80,20 +81,68 @@ fn deleting_a_span_zeroes_every_row_and_keeps_neighbours() {
}
#[test]
fn explicit_growth_feeds_the_span_allocator_and_relocation_keeps_content() {
fn relocation_is_staged_until_the_writer_commits() {
let mut data = RenderData::new(4096).unwrap();
data.create_rows("a".into(), 1, 16, "u32".into()).unwrap();
data.create_rows("b".into(), 1, 16, "u32".into()).unwrap();
assert_eq!(data.allocate_object("a", 1).unwrap(), (0, false));
let base = (data.rows("a").unwrap().offset - data.base) as usize + data.arena_start;
assert_eq!(data.allocate_object("a", 1).unwrap(), (0, false, false));
let old = data.rows("a").unwrap().clone();
let base = (old.offset - data.base) as usize + data.arena_start;
data.arena[base..base + 16].fill(0xCD);
let grown = data.create_rows("a".into(), 5, 16, "u32".into()).unwrap();
assert_eq!(grown.rows, 5);
assert!(data.bytes("a").unwrap()[..16]
.iter()
.all(|byte| *byte == 0xCD));
// "b" sits right behind "a", so growing "a" must move it: the worker stages a zeroed region
// and keeps rendering from (and the arena owning) the old bytes.
let (staged, moved) = data.create_rows("a".into(), 5, 16, "u32".into()).unwrap();
assert!(moved);
assert_eq!(staged.rows, 5);
assert_ne!(staged.offset, old.offset);
assert_eq!(data.rows("a").unwrap().offset, old.offset);
assert_eq!(data.staged("a").unwrap().offset, staged.offset);
assert_eq!(
data.create_rows("a".into(), 6, 16, "u32".into()),
Err("ROWS_PENDING")
);
assert_eq!(data.delete_rows("a"), Err("ROWS_PENDING"));
assert!(!data.free.take(old.offset - data.base, old.bytes));
// The writer keeps writing the old rows until it copies them over and commits.
data.arena[base + 4..base + 8].fill(0xEF);
let target = (staged.offset - data.base) as usize + data.arena_start;
data.arena.copy_within(base..base + 16, target);
data.commit(&["a".into()]).unwrap();
assert_eq!(data.rows("a").unwrap().offset, staged.offset);
assert_eq!(
&data.bytes("a").unwrap()[..8],
&[0xCD, 0xCD, 0xCD, 0xCD, 0xEF, 0xEF, 0xEF, 0xEF]
);
assert!(data.bytes("a").unwrap()[16..].iter().all(|byte| *byte == 0));
assert_eq!(data.allocate_object("a", 4).unwrap(), (1, false));
assert_eq!(data.allocate_object("a", 1).unwrap(), (5, true));
assert!(data.free.take(old.offset - data.base, old.bytes));
assert_eq!(data.commit(&["a".into()]), Err("ROWS_PENDING"));
// Newly staged rows already feed the span allocator; the writer owns the staged region.
data.create_rows("c".into(), 1, 16, "u32".into()).unwrap();
assert_eq!(data.allocate_object("a", 4).unwrap(), (1, false, false));
let (id, grew, moved) = data.allocate_object("a", 1).unwrap();
assert_eq!((id, grew, moved), (5, true, true));
assert_eq!(data.staged("a").unwrap().rows, 10);
assert_eq!(data.rows("a").unwrap().rows, 5);
data.commit(&["a".into()]).unwrap();
assert_eq!(rows_of(&data, "a"), 10);
}
#[test]
fn discarding_a_staged_relocation_restores_capacity() {
let mut data = RenderData::new(4096).unwrap();
data.create_rows("a".into(), 2, 16, "u32".into()).unwrap();
data.create_rows("b".into(), 1, 16, "u32".into()).unwrap();
assert_eq!(data.allocate_object("a", 2).unwrap(), (0, false, false));
let (staged, moved) = data.create_rows("a".into(), 8, 16, "u32".into()).unwrap();
assert!(moved);
data.discard_pending();
assert_eq!(rows_of(&data, "a"), 2);
assert!(data.free.take(staged.offset - data.base, staged.bytes));
// Rows 2..8 must not be handed out: they only existed in the discarded region.
assert_eq!(data.allocate_object("a", 1).unwrap(), (2, true, false));
assert_eq!(rows_of(&data, "a"), 4);
}