Skip to content

Commit

Permalink
piecrust: use crumbles::Mmap::set_len
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo Leegwater Simões committed Sep 7, 2023
1 parent 7208a45 commit 7989077
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
4 changes: 4 additions & 0 deletions piecrust/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## Changed

- Change to use `crumbles::Mmap::set_len` on growing memory

## [0.9.1] - 2023-09-07

### Changed
Expand Down
29 changes: 18 additions & 11 deletions piecrust/src/store/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// Copyright (c) DUSK NETWORK. All rights reserved.

use std::fs::OpenOptions;
use std::io;
use std::ops::{Deref, DerefMut};
use std::path::Path;
use std::ptr::NonNull;
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::{cmp, io};

use crumbles::{Mmap, PAGE_SIZE};
use wasmer::WASM_MAX_PAGES;
Expand All @@ -19,15 +19,14 @@ use wasmer_vm::{
VMMemory, VMMemoryDefinition,
};

const MIN_PAGES: usize = 2;
const MIN_PAGES: usize = 3;
const MIN_MEM_SIZE: usize = MIN_PAGES * PAGE_SIZE;
const MAX_PAGES: usize = WASM_MAX_PAGES as usize;
pub const MAX_MEM_SIZE: usize = MAX_PAGES * PAGE_SIZE;

#[derive(Debug)]
pub(crate) struct MemoryInner {
pub(crate) mmap: Mmap,
pub(crate) current_len: usize,
def: VMMemoryDefinition,
init: bool,
}
Expand All @@ -42,15 +41,16 @@ impl Memory {
pub(crate) fn new() -> io::Result<Self> {
let mut mmap = Mmap::new()?;

mmap.set_len(MIN_MEM_SIZE as u32);

let def = VMMemoryDefinition {
base: mmap.as_mut_ptr(),
current_length: MAX_MEM_SIZE,
current_length: MIN_MEM_SIZE + PAGE_SIZE,
};

Ok(Self {
inner: Arc::new(RwLock::new(MemoryInner {
mmap,
current_len: MIN_MEM_SIZE,
def,
init: false,
})),
Expand All @@ -72,15 +72,16 @@ impl Memory {
}))?
};

mmap.set_len(len as u32);

let def = VMMemoryDefinition {
base: mmap.as_mut_ptr(),
current_length: MAX_MEM_SIZE,
current_length: cmp::min(len + PAGE_SIZE, MAX_MEM_SIZE),
};

Ok(Self {
inner: Arc::new(RwLock::new(MemoryInner {
mmap,
current_len: len,
def,
init: true,
})),
Expand Down Expand Up @@ -156,21 +157,21 @@ impl LinearMemory for Memory {
}

fn size(&self) -> Pages {
let pages = self.read().inner.current_len / PAGE_SIZE;
let pages = self.read().len() / PAGE_SIZE;
Pages(pages as u32)
}

fn style(&self) -> MemoryStyle {
MemoryStyle::Static {
bound: Pages(MAX_PAGES as u32),
offset_guard_size: PAGE_SIZE as u64,
offset_guard_size: 0,
}
}

fn grow(&mut self, delta: Pages) -> Result<Pages, MemoryError> {
let mut memory = self.write();

let current_len = memory.inner.current_len;
let current_len = memory.len();
let new_len = current_len + delta.0 as usize * PAGE_SIZE;

if new_len > MAX_PAGES * PAGE_SIZE {
Expand All @@ -180,7 +181,13 @@ impl LinearMemory for Memory {
});
}

memory.inner.current_len = new_len;
memory.set_len(new_len as u32);

memory.inner.def = VMMemoryDefinition {
base: memory.as_mut_ptr(),
current_length: cmp::min(new_len + PAGE_SIZE, MAX_MEM_SIZE),
};

Ok(Pages((new_len / PAGE_SIZE) as u32))
}

Expand Down
2 changes: 1 addition & 1 deletion piecrust/src/store/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl ContractIndex {
let memory = memory.read();
let memory_inner = memory.inner;

element.len = memory_inner.current_len;
element.len = memory_inner.mmap.len();

for (dirty_page, _, page_offset) in memory_inner.mmap.dirty_pages() {
element.offsets.insert(page_offset);
Expand Down

0 comments on commit 7989077

Please sign in to comment.