Skip to content

Commit

Permalink
fix(zkvm): optimize memory usage for low memory configurations
Browse files Browse the repository at this point in the history
Co-Authored-By: John Guibas <[email protected]>
  • Loading branch information
devin-ai-integration[bot] and jtguibas committed Dec 12, 2024
1 parent 9569a6e commit dfebebf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/zkvm/entrypoint/src/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::sync::atomic::{AtomicUsize, Ordering};
use crate::syscalls::sys_alloc_aligned;

/// A block in our free list
#[repr(C)]
#[repr(C, align(8))]
struct FreeBlock {
size: usize,
next: Option<NonNull<FreeBlock>>,
Expand Down
7 changes: 5 additions & 2 deletions crates/zkvm/entrypoint/src/syscalls/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// Memory addresses must be lower than BabyBear prime.
const MAX_MEMORY: usize = 0x78000000;

const RESERVED_SIZE: usize = 1024 * 1024; // 1MB reserved section
const RESERVED_SIZE: usize = MAX_MEMORY / 64;
static mut RESERVED_POS: usize = 0;
static mut RESERVED_START: usize = 0;

Expand All @@ -37,6 +37,10 @@ pub unsafe extern "C" fn sys_alloc_aligned(bytes: usize, align: usize) -> *mut u
if heap_pos == 0 {
heap_pos = unsafe { (&_end) as *const u8 as usize };
unsafe {
// Check if reserved section would exceed memory limit
if heap_pos + RESERVED_SIZE > MAX_MEMORY {
panic!("Reserved section would exceed memory limit");
}
RESERVED_START = heap_pos;
RESERVED_POS = heap_pos;
heap_pos += RESERVED_SIZE;
Expand All @@ -63,7 +67,6 @@ pub unsafe extern "C" fn sys_alloc_aligned(bytes: usize, align: usize) -> *mut u
pub unsafe extern "C" fn sys_alloc_reserved(bytes: usize, align: usize) -> *mut u8 {
let mut pos = RESERVED_POS;

// Align the position
let offset = pos & (align - 1);
if offset != 0 {
pos += align - offset;
Expand Down

0 comments on commit dfebebf

Please sign in to comment.