Skip to content

Commit 9eef85b

Browse files
authored
perf(vm): avoid zeroed allocation in BasicMemory::clone (#2352)
Replace `alloc_zeroed` with `alloc` in `BasicMemory::clone` to avoid unnecessary zero-initialization before copying the existing contents. The clone path always overwrites the entire allocated region with `copy_nonoverlapping`, so zeroing first provides no safety benefit but doubles the amount of memory work for large buffers. The constructor still uses `alloc_zeroed` to preserve the invariant that freshly created linear memory starts zeroed.
1 parent d24a91a commit 9eef85b

File tree

1 file changed

+3
-3
lines changed
  • crates/vm/src/system/memory/online

1 file changed

+3
-3
lines changed

crates/vm/src/system/memory/online/basic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{
2-
alloc::{alloc_zeroed, dealloc, Layout},
2+
alloc::{alloc, alloc_zeroed, dealloc, Layout},
33
ptr::NonNull,
44
};
55

@@ -55,11 +55,11 @@ impl Clone for BasicMemory {
5555

5656
let layout = self.layout;
5757
// SAFETY:
58-
// - alloc_zeroed creates a valid allocation for the layout
58+
// - alloc creates a valid allocation for the layout
5959
// - copy_nonoverlapping copies exactly self.size bytes from valid source to valid dest
6060
// - new_ptr is guaranteed non-null after alloc check
6161
let ptr = unsafe {
62-
let new_ptr = alloc_zeroed(layout);
62+
let new_ptr = alloc(layout);
6363
if new_ptr.is_null() {
6464
std::alloc::handle_alloc_error(layout);
6565
}

0 commit comments

Comments
 (0)