Skip to content

Commit 723acbf

Browse files
array arena (#1614)
1 parent 13ba358 commit 723acbf

14 files changed

Lines changed: 544 additions & 800 deletions

File tree

src/executor.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
SEGMENT_ARENA_BUILTIN_SIZE,
1515
},
1616
native_panic,
17-
runtime::{BLAKE_CALL_COUNT, BOX_ARENA, BUILTIN_COSTS},
17+
runtime::{BLAKE_CALL_COUNT, BUILTIN_COSTS, EXECUTION_ARENA},
1818
starknet::{handler::StarknetSyscallHandlerCallbacks, StarknetSyscallHandler},
1919
types::TypeBuilder,
2020
utils::{BuiltinCosts, RangeExt},
@@ -364,7 +364,7 @@ fn invoke_dynamic(
364364
pub(crate) struct InvocationGuard {
365365
builtin_costs: BuiltinCosts,
366366
blake_call_count: u64,
367-
box_arena: Bump,
367+
execution_arena: Bump,
368368
#[cfg(feature = "with-cheatcode")]
369369
syscall_handler: Option<*mut ()>,
370370
}
@@ -379,7 +379,8 @@ impl InvocationGuard {
379379
Self {
380380
builtin_costs: BUILTIN_COSTS.replace(builtin_costs),
381381
blake_call_count: BLAKE_CALL_COUNT.with(|c| c.replace(0)),
382-
box_arena: BOX_ARENA.with(|c| std::mem::replace(&mut *c.borrow_mut(), Bump::new())),
382+
execution_arena: EXECUTION_ARENA
383+
.with(|c| std::mem::replace(&mut *c.borrow_mut(), Bump::new())),
383384
#[cfg(feature = "with-cheatcode")]
384385
syscall_handler: syscall_handler.map(|ptr| {
385386
let previous_value = crate::starknet::SYSCALL_HANDLER_VTABLE.get();
@@ -394,7 +395,7 @@ impl Drop for InvocationGuard {
394395
fn drop(&mut self) {
395396
BUILTIN_COSTS.set(self.builtin_costs);
396397
BLAKE_CALL_COUNT.with(|c| c.set(self.blake_call_count));
397-
BOX_ARENA.with(|c| std::mem::swap(&mut *c.borrow_mut(), &mut self.box_arena));
398+
EXECUTION_ARENA.with(|c| std::mem::swap(&mut *c.borrow_mut(), &mut self.execution_arena));
398399
#[cfg(feature = "with-cheatcode")]
399400
if let Some(previous_value) = self.syscall_handler {
400401
crate::starknet::SYSCALL_HANDLER_VTABLE.set(previous_value);

src/executor/contract.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use crate::{
5353
types::{array::ArrayMetadata, TypeBuilder},
5454
utils::{
5555
decode_error_message, generate_function_name, get_integer_layout, get_types_total_size,
56-
libc_free, libc_malloc, BuiltinCosts,
56+
BuiltinCosts,
5757
},
5858
OptLevel,
5959
};
@@ -499,7 +499,12 @@ impl AotContractExecutor {
499499

500500
let data_ptr = match args.len() {
501501
0 => std::ptr::null_mut(),
502-
_ => unsafe { libc_malloc(felt_layout.size() * args.len()).cast::<u8>() },
502+
_ => unsafe {
503+
crate::runtime::cairo_native__arena_alloc(
504+
(felt_layout.size() * args.len()) as u64,
505+
felt_layout.align() as u64,
506+
)
507+
},
503508
};
504509

505510
for (idx, elem) in args.iter().enumerate() {
@@ -513,14 +518,17 @@ impl AotContractExecutor {
513518
};
514519
}
515520

516-
// Allocate metadata struct: { refcount: u32, max_len: u32, data_ptr: *mut () }
521+
// Allocate metadata struct: { max_len: u32, data_ptr: *mut () }
517522
let metadata_ptr = if data_ptr.is_null() {
518523
ptr::null_mut()
519524
} else {
520525
unsafe {
521-
let metadata = libc_malloc(size_of::<ArrayMetadata>()).cast::<ArrayMetadata>();
526+
let metadata = crate::runtime::cairo_native__arena_alloc(
527+
size_of::<ArrayMetadata>() as u64,
528+
align_of::<ArrayMetadata>() as u64,
529+
)
530+
.cast::<ArrayMetadata>();
522531
metadata.write(ArrayMetadata {
523-
refcount: 1,
524532
max_len: len_u32,
525533
data_ptr,
526534
});
@@ -679,15 +687,6 @@ impl AotContractExecutor {
679687

680688
array_value.push(Felt::from_bytes_le(&data));
681689
}
682-
683-
unsafe {
684-
native_assert!(
685-
metadata.refcount == 1,
686-
"return array should have a reference count of 1"
687-
);
688-
libc_free(data_ptr.cast());
689-
libc_free(metadata_ptr.cast());
690-
}
691690
}
692691

693692
let error_msg = match tag {

0 commit comments

Comments
 (0)