From ccfa245261bea22b2a5ef381a915606f6e1773d0 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Mon, 31 Jan 2022 17:03:42 -0800 Subject: [PATCH] Optimization: only mprotect the *new* bit of heap, not all of it. (This was not a correctness bug, but is an obvious performance bug...) --- crates/runtime/src/memfd.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/runtime/src/memfd.rs b/crates/runtime/src/memfd.rs index 1740aba324..2a0aea640e 100644 --- a/crates/runtime/src/memfd.rs +++ b/crates/runtime/src/memfd.rs @@ -316,7 +316,12 @@ impl MemFdSlot { } pub(crate) fn set_heap_limit(&mut self, size_bytes: usize) -> Result<()> { - assert!(size_bytes > self.cur_size); + assert!( + size_bytes > self.cur_size, + "size_bytes = {} cur_size = {}", + size_bytes, + self.cur_size + ); // mprotect the relevant region. let start = self.base + self.cur_size; let len = size_bytes - self.cur_size; @@ -327,6 +332,7 @@ impl MemFdSlot { rustix::io::MprotectFlags::READ | rustix::io::MprotectFlags::WRITE, )?; } + self.cur_size = size_bytes; Ok(()) } @@ -355,6 +361,7 @@ impl MemFdSlot { == maybe_image.as_ref().unwrap().fd.as_file().as_raw_fd()) { self.dirty = true; + self.cur_size = initial_size_bytes; return Ok(()); } @@ -405,6 +412,7 @@ impl MemFdSlot { // mprotect above `initial_size_bytes`. self.initial_size = initial_size_bytes; + self.cur_size = initial_size_bytes; self.protect_past_initial_size() .map_err(|e| InstantiationError::Resource(e.into()))?;