improve performance
This commit is contained in:
@@ -27,16 +27,24 @@ pub struct Verifier;
|
||||
impl Verifier {
|
||||
pub fn verify(code: &[u8], functions: &[FunctionMeta]) -> Result<Vec<u16>, VerifierError> {
|
||||
let mut max_stacks = Vec::with_capacity(functions.len());
|
||||
// Precompute function [start, end) ranges once for O(1) lookups
|
||||
let layouts = layout::compute_function_layouts(functions, code.len());
|
||||
for (i, func) in functions.iter().enumerate() {
|
||||
max_stacks.push(Self::verify_function(code, func, i, functions)?);
|
||||
max_stacks.push(Self::verify_function(code, func, i, functions, &layouts)?);
|
||||
}
|
||||
Ok(max_stacks)
|
||||
}
|
||||
|
||||
fn verify_function(code: &[u8], func: &FunctionMeta, func_idx: usize, all_functions: &[FunctionMeta]) -> Result<u16, VerifierError> {
|
||||
fn verify_function(
|
||||
code: &[u8],
|
||||
func: &FunctionMeta,
|
||||
func_idx: usize,
|
||||
all_functions: &[FunctionMeta],
|
||||
layouts: &[layout::FunctionLayout],
|
||||
) -> Result<u16, VerifierError> {
|
||||
let func_start = func.code_offset as usize;
|
||||
// Use o cálculo canônico compartilhado com o compiler/linker
|
||||
let func_end = layout::function_end_from_next(all_functions, func_idx, code.len());
|
||||
// Use precomputed canonical range end
|
||||
let func_end = layouts.get(func_idx).map(|l| l.end).unwrap_or_else(|| code.len());
|
||||
|
||||
if func_start > code.len() || func_end > code.len() || func_start > func_end {
|
||||
return Err(VerifierError::FunctionOutOfBounds {
|
||||
@@ -135,7 +143,7 @@ impl Verifier {
|
||||
if spec.is_branch {
|
||||
// Canonical contract: branch immediate is RELATIVE to function start.
|
||||
let target_rel = instr.imm_u32().unwrap() as usize;
|
||||
let func_end_abs = layout::function_end_from_next(all_functions, func_idx, code.len());
|
||||
let func_end_abs = layouts.get(func_idx).map(|l| l.end).unwrap_or_else(|| code.len());
|
||||
let func_len = func_end_abs - func_start;
|
||||
|
||||
if target_rel > func_len {
|
||||
@@ -187,7 +195,7 @@ impl Verifier {
|
||||
|
||||
if !spec.is_terminator {
|
||||
let next_pc = instr.next_pc;
|
||||
let func_len = layout::function_len_from_next(all_functions, func_idx, code.len());
|
||||
let func_len = layouts.get(func_idx).map(|l| l.end - l.start).unwrap_or_else(|| 0);
|
||||
if next_pc < func_len {
|
||||
if let Some(&existing_height) = stack_height_in.get(&next_pc) {
|
||||
if existing_height != out_height {
|
||||
|
||||
Reference in New Issue
Block a user