dev/perf-runtime-telemetry-hot-path (#13)
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
Reviewed-on: #13 Co-authored-by: bQUARKz <bquarkz@gmail.com> Co-committed-by: bQUARKz <bquarkz@gmail.com>
This commit was merged in pull request #13.
This commit is contained in:
@@ -2,6 +2,9 @@ use crate::call_frame::CallFrame;
|
||||
use crate::object::{ObjectHeader, ObjectKind};
|
||||
use prometeu_bytecode::{HeapRef, Value};
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
/// Internal stored object: header plus opaque payload bytes.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct StoredObject {
|
||||
@@ -22,6 +25,30 @@ pub struct StoredObject {
|
||||
pub coroutine: Option<CoroutineData>,
|
||||
}
|
||||
|
||||
impl StoredObject {
|
||||
/// Returns the approximate memory footprint of this object in bytes.
|
||||
pub fn bytes(&self) -> usize {
|
||||
let mut total = std::mem::size_of::<ObjectHeader>();
|
||||
total += self.payload.capacity();
|
||||
|
||||
if let Some(elems) = &self.array_elems {
|
||||
total += std::mem::size_of::<Vec<Value>>();
|
||||
total += elems.capacity() * std::mem::size_of::<Value>();
|
||||
}
|
||||
if let Some(env) = &self.closure_env {
|
||||
total += std::mem::size_of::<Vec<Value>>();
|
||||
total += env.capacity() * std::mem::size_of::<Value>();
|
||||
}
|
||||
if let Some(coro) = &self.coroutine {
|
||||
total += std::mem::size_of::<CoroutineData>();
|
||||
total += coro.stack.capacity() * std::mem::size_of::<Value>();
|
||||
total += coro.frames.capacity() * std::mem::size_of::<CallFrame>();
|
||||
}
|
||||
|
||||
total
|
||||
}
|
||||
}
|
||||
|
||||
/// Execution state of a coroutine.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||
pub enum CoroutineState {
|
||||
@@ -49,14 +76,22 @@ pub struct Heap {
|
||||
objects: Vec<Option<StoredObject>>,
|
||||
// Reclaimed slots available for deterministic reuse (LIFO).
|
||||
free_list: Vec<usize>,
|
||||
|
||||
/// Total bytes currently used by all objects in the heap.
|
||||
pub used_bytes: Arc<AtomicUsize>,
|
||||
}
|
||||
|
||||
impl Heap {
|
||||
pub fn new() -> Self {
|
||||
Self { objects: Vec::new(), free_list: Vec::new() }
|
||||
Self {
|
||||
objects: Vec::new(),
|
||||
free_list: Vec::new(),
|
||||
used_bytes: Arc::new(AtomicUsize::new(0)),
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_object(&mut self, obj: StoredObject) -> HeapRef {
|
||||
self.used_bytes.fetch_add(obj.bytes(), Ordering::Relaxed);
|
||||
if let Some(idx) = self.free_list.pop() {
|
||||
debug_assert!(self.objects.get(idx).is_some_and(|slot| slot.is_none()));
|
||||
self.objects[idx] = Some(obj);
|
||||
@@ -363,6 +398,7 @@ impl Heap {
|
||||
obj.header.set_marked(false);
|
||||
} else {
|
||||
// Unreachable: reclaim by dropping and turning into tombstone.
|
||||
self.used_bytes.fetch_sub(obj.bytes(), Ordering::Relaxed);
|
||||
*slot = None;
|
||||
self.free_list.push(idx);
|
||||
}
|
||||
|
||||
@@ -146,10 +146,10 @@ impl Verifier {
|
||||
|
||||
let func_code = &code[func_start..func_end];
|
||||
|
||||
// Funções vazias (sem qualquer byte de código) são consideradas válidas no verificador.
|
||||
// Elas não consomem nem produzem valores na pilha e não possuem fluxo interno.
|
||||
// Observação: se uma função vazia for chamada em tempo de execução e retorno/efeitos
|
||||
// forem esperados, caberá ao gerador de código/linker impedir tal situação.
|
||||
// Empty functions (no code bytes) are considered valid in the verifier.
|
||||
// They do not consume or produce values on the stack and have no internal flow.
|
||||
// Note: if an empty function is called at runtime and return/effects
|
||||
// are expected, it is the responsibility of the code generator/linker to prevent this situation.
|
||||
if func_code.is_empty() {
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
@@ -140,6 +140,11 @@ impl VirtualMachine {
|
||||
self.operand_stack[start..].iter().rev().cloned().collect()
|
||||
}
|
||||
|
||||
/// Returns a reference to the VM's heap.
|
||||
pub fn heap(&self) -> &Heap {
|
||||
&self.heap
|
||||
}
|
||||
|
||||
/// Returns true if the VM has executed a HALT and is not currently running.
|
||||
pub fn is_halted(&self) -> bool {
|
||||
self.halted
|
||||
|
||||
Reference in New Issue
Block a user