perf(registry): record bounded layout load and GC evidence for PLN-0176 step 35

This commit is contained in:
2026-10-07 21:36:16 +01:00
parent c6fff08e7c
commit dd3594240e
5 changed files with 316 additions and 2 deletions
@@ -258,3 +258,31 @@ fn constant_strings_share_image_domain_across_sessions_and_native_snapshots() {
let Value::String(text) = &snapshot[0] else { panic!() };
assert_eq!(text.as_ref(), "shared");
}
#[test]
#[ignore = "explicit release measurement; not a timing assertion"]
fn sealed_image_load_probe() {
use std::time::Instant;
let constants: Vec<_> =
(0..512).map(|index| K::String(format!("{index:04}:{}", "x".repeat(59)))).collect();
let bytes = fixture(&[1, 0], 0, &constants, None, &[]);
let mut budget = budget();
let start = Instant::now();
let image = ValidatedProgramImage::load(&bytes, &mut budget, caps::NONE).unwrap();
let elapsed = start.elapsed().as_nanos();
assert_eq!(image.constant_content(), (32768, 512));
assert_eq!(budget.used(Q::TemporaryBytes), 0);
println!(
"image arch={} pbx={} registry={} payload={} work={} peak_scratch={} retained_scratch={} strings={} allocations={} load_ns={}",
std::env::consts::ARCH,
bytes.len(),
image.registry_bytes(),
image.payload_bytes(),
budget.used(Q::Work),
budget.peak_usage(Q::TemporaryBytes),
budget.used(Q::TemporaryBytes),
image.constant_content().0,
image.constant_content().1,
elapsed
);
}
@@ -526,3 +526,156 @@ fn typed_dynamic_self_cycle_uses_one_mark_worklist_entry() {
heap.sweep();
assert_eq!(budget.used(Q::GuestBytes), 0);
}
#[test]
#[ignore = "explicit release measurement; not a timing assertion"]
fn storage_and_gc_probe() {
use crate::value_contract::{pack_dynamic, unpack_dynamic};
use std::hint::black_box;
use std::time::Instant;
println!(
"native arch={} Value={}/{} HeapRef={}/{} StringSlot={}/{} DynamicSlot={}/{} TraitSlot={}/{} StoredObject={}/{}",
std::env::consts::ARCH,
size_of::<Value>(),
align_of::<Value>(),
size_of::<HeapRef>(),
align_of::<HeapRef>(),
size_of::<StringSlot>(),
align_of::<StringSlot>(),
size_of::<DynamicSlot>(),
align_of::<DynamicSlot>(),
size_of::<TraitSlot>(),
align_of::<TraitSlot>(),
size_of::<StoredObject>(),
align_of::<StoredObject>()
);
let registry = fixture_registry();
for array in [INTS, BOOLS, VECS, PIXELS, REFS, STRINGS, TRAITS, DYNAMICS] {
let budget = RuntimeBudget::default();
let mut heap = Heap::with_budget(&budget);
let layout = registry.type_layout(array).unwrap().array.unwrap();
let count = 4096;
let start = Instant::now();
let reference = heap.allocate_typed_array(registry.clone(), array, count).unwrap();
let allocation_ns = start.elapsed().as_nanos();
let default =
registry.type_layout(layout.element_type_id).unwrap().storage.default_values();
assert_eq!(heap.load_typed(&registry, reference, 4095).unwrap().as_slice(), default);
let start = Instant::now();
for index in 0..100_000 {
black_box(
heap.load_typed(black_box(&registry), reference, index % count as usize).unwrap(),
);
}
let access_ns = start.elapsed().as_nanos();
println!(
"array type={} count={} stride={} align={} guest={} aux={} handles={} allocation_ns={} reads=100000 access_ns={}",
array.0,
count,
layout.stride,
layout.element_alignment,
budget.used(Q::GuestBytes),
budget.used(Q::AuxiliaryBytes),
budget.used(Q::HandleSlots),
allocation_ns,
access_ns
);
heap.mark_from_roots([reference]).unwrap();
heap.sweep();
assert_eq!(heap.len(), 1);
heap.sweep();
assert_eq!(budget.used(Q::GuestBytes), 0);
drop(heap);
assert_eq!(budget.used(Q::AuxiliaryBytes), 0);
}
for period in [1, 16, 256] {
let mut declarations = registry.declarations().clone();
let ty = TypeId(declarations.types.len() as u32);
declarations.types.push(TypeDecl {
name: None,
kind: TypeKind::Struct(
(0..4096)
.map(|index| NamedType {
name: None,
type_id: if index % period == 0 { ENEMY } else { I64 },
})
.collect(),
),
});
let mut load = LoadBudget::new(Default::default());
let registry = Arc::new(Registry::resolve(declarations, 0, &mut load).unwrap());
let budget = RuntimeBudget::default();
let mut heap = Heap::with_budget(&budget);
let holder = heap.allocate_typed_struct(registry.clone(), ty).unwrap();
let child = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
let dead = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
for index in (0..4096).step_by(period) {
heap.store_typed(&registry, holder, index, &[Value::HeapRef(child)]).unwrap();
}
let start = Instant::now();
for _ in 0..1000 {
heap.mark_from_roots([holder]).unwrap();
heap.sweep();
}
let gc_ns = start.elapsed().as_nanos();
assert_eq!(heap.len(), 2);
assert!(heap.is_valid(child));
assert!(!heap.is_valid(dead));
assert_eq!(budget.used(Q::TemporaryBytes), 0);
println!(
"gc period={} fields=4096 roots={} live={} guest={} aux={} work={} load_peak_scratch={} rounds=1000 gc_ns={}",
period,
4096 / period,
heap.len(),
budget.used(Q::GuestBytes),
budget.used(Q::AuxiliaryBytes),
load.used(prometeu_bytecode::load_limits::LoadResource::Work),
load.peak_usage(prometeu_bytecode::load_limits::LoadResource::TemporaryBytes),
gc_ns
);
}
let budget = RuntimeBudget::default();
let mut heap = Heap::with_budget(&budget);
let start = Instant::now();
let dynamic =
pack_dynamic(&registry, &mut heap, VEC, &[Value::Float(3.0), Value::Float(4.0)]).unwrap();
let pack_ns = start.elapsed().as_nanos();
let start = Instant::now();
for _ in 0..100_000 {
black_box(unpack_dynamic(&registry, &heap, VEC, black_box(dynamic.as_slice())).unwrap());
}
println!(
"dynamic vec2_boxes={} guest={} pack_ns={} unpacks=100000 unpack_ns={}",
heap.len(),
budget.used(Q::GuestBytes),
pack_ns,
start.elapsed().as_nanos()
);
let holder = heap.allocate_typed_array(registry.clone(), DYNAMICS, 1).unwrap();
heap.store_typed(&registry, holder, 0, dynamic.as_slice()).unwrap();
heap.mark_from_roots([holder]).unwrap();
heap.sweep();
assert_eq!(heap.len(), 2);
heap.sweep();
assert_eq!(budget.used(Q::GuestBytes), 0);
let strings = crate::string_ledger::StringLedger::new(budget.clone()).unwrap();
let text = strings.import("shared-native").unwrap();
let weak = Arc::downgrade(&text);
let root = heap.allocate_typed_struct(registry.clone(), MIXED).unwrap();
heap.store_typed(&registry, root, 3, &[Value::String(text.clone())]).unwrap();
heap.store_typed(&registry, root, 4, &[Value::Int64(STRING.0.into()), Value::String(text)])
.unwrap();
assert_eq!(budget.used(Q::StringBytes), 13);
heap.mark_from_roots([root]).unwrap();
heap.sweep();
strings.reconcile();
assert!(weak.upgrade().is_some());
assert_eq!(budget.used(Q::StringBytes), 13);
heap.sweep();
strings.reconcile();
assert!(weak.upgrade().is_none());
assert_eq!(budget.used(Q::StringBytes), 0);
println!(
"native_string content=13 storage_owners=2 allocations=1 rooted_gc=retained unrooted_gc=released"
);
}