memfd: Some minor follow-ups (#3759)
* Tweak memfd-related features crates This commit changes the `memfd` feature for the `wasmtime-cli` crate from an always-on feature to a default-on feature which can be disabled at compile time. Additionally the `pooling-allocator` feature is also given similar treatment. Additionally some documentation was added for the `memfd` feature on the `wasmtime` crate. * Don't store `Arc<T>` in `InstanceAllocationRequest` Instead store `&Arc<T>` to avoid having the clone that lives in `InstanceAllocationRequest` not actually going anywhere. Otherwise all instance allocation requires an extra clone to create it for the request and an extra decrement when the request goes away. Internally clones are made as necessary when creating instances. * Enable the pooling allocator by default for `wasmtime-cli` While perhaps not the most useful option since the CLI doesn't have a great way to take advantage of this it probably makes sense to at least match the features of `wasmtime` itself. * Fix some lints and issues * More compile fixes
This commit is contained in:
@@ -33,7 +33,7 @@ pub use self::pooling::{
|
||||
/// Represents a request for a new runtime instance.
|
||||
pub struct InstanceAllocationRequest<'a> {
|
||||
/// The module being instantiated.
|
||||
pub module: Arc<Module>,
|
||||
pub module: &'a Arc<Module>,
|
||||
|
||||
/// The unique ID of the module being allocated within this engine.
|
||||
pub unique_id: Option<CompiledModuleId>,
|
||||
@@ -42,7 +42,7 @@ pub struct InstanceAllocationRequest<'a> {
|
||||
pub image_base: usize,
|
||||
|
||||
/// If using MemFD-based memories, the backing MemFDs.
|
||||
pub memfds: Option<Arc<ModuleMemFds>>,
|
||||
pub memfds: Option<&'a Arc<ModuleMemFds>>,
|
||||
|
||||
/// Descriptors about each compiled function, such as the offset from
|
||||
/// `image_base`.
|
||||
@@ -672,7 +672,7 @@ impl OnDemandInstanceAllocator {
|
||||
&self,
|
||||
module: &Module,
|
||||
store: &mut StorePtr,
|
||||
memfds: &Option<Arc<ModuleMemFds>>,
|
||||
memfds: Option<&Arc<ModuleMemFds>>,
|
||||
) -> Result<PrimaryMap<DefinedMemoryIndex, Memory>, InstantiationError> {
|
||||
let creator = self
|
||||
.mem_creator
|
||||
@@ -686,9 +686,7 @@ impl OnDemandInstanceAllocator {
|
||||
let defined_memory_idx = module
|
||||
.defined_memory_index(memory_idx)
|
||||
.expect("Skipped imports, should never be None");
|
||||
let memfd_image = memfds
|
||||
.as_ref()
|
||||
.and_then(|memfds| memfds.get_memory_image(defined_memory_idx));
|
||||
let memfd_image = memfds.and_then(|memfds| memfds.get_memory_image(defined_memory_idx));
|
||||
|
||||
memories.push(
|
||||
Memory::new_dynamic(
|
||||
@@ -723,7 +721,7 @@ unsafe impl InstanceAllocator for OnDemandInstanceAllocator {
|
||||
&self,
|
||||
mut req: InstanceAllocationRequest,
|
||||
) -> Result<InstanceHandle, InstantiationError> {
|
||||
let memories = self.create_memories(&req.module, &mut req.store, &req.memfds)?;
|
||||
let memories = self.create_memories(&req.module, &mut req.store, req.memfds)?;
|
||||
let tables = Self::create_tables(&req.module, &mut req.store)?;
|
||||
|
||||
let host_state = std::mem::replace(&mut req.host_state, Box::new(()));
|
||||
|
||||
@@ -378,7 +378,7 @@ impl InstancePool {
|
||||
index,
|
||||
instance,
|
||||
&self.memories,
|
||||
&req.memfds,
|
||||
req.memfds,
|
||||
self.memories.max_wasm_pages,
|
||||
)?;
|
||||
|
||||
@@ -506,7 +506,7 @@ impl InstancePool {
|
||||
instance_idx: usize,
|
||||
instance: &mut Instance,
|
||||
memories: &MemoryPool,
|
||||
maybe_memfds: &Option<Arc<ModuleMemFds>>,
|
||||
maybe_memfds: Option<&Arc<ModuleMemFds>>,
|
||||
max_pages: u64,
|
||||
) -> Result<(), InstantiationError> {
|
||||
let module = instance.module.as_ref();
|
||||
@@ -1468,7 +1468,7 @@ mod test {
|
||||
handles.push(
|
||||
instances
|
||||
.allocate(InstanceAllocationRequest {
|
||||
module: module.clone(),
|
||||
module: &module,
|
||||
unique_id: None,
|
||||
image_base: 0,
|
||||
functions,
|
||||
@@ -1494,7 +1494,7 @@ mod test {
|
||||
);
|
||||
|
||||
match instances.allocate(InstanceAllocationRequest {
|
||||
module: module.clone(),
|
||||
module: &module,
|
||||
unique_id: None,
|
||||
functions,
|
||||
image_base: 0,
|
||||
|
||||
@@ -579,7 +579,7 @@ mod test {
|
||||
handles.push(
|
||||
instances
|
||||
.allocate(InstanceAllocationRequest {
|
||||
module: module.clone(),
|
||||
module: &module,
|
||||
memfds: None,
|
||||
unique_id: None,
|
||||
image_base: 0,
|
||||
|
||||
@@ -471,6 +471,7 @@ impl MemFdSlot {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(dead_code)] // ignore warnings as this is only used in some cfgs
|
||||
pub(crate) fn clear_and_remain_ready(&mut self) -> Result<()> {
|
||||
assert!(self.dirty);
|
||||
// madvise the image range. This will throw away dirty pages,
|
||||
@@ -511,6 +512,7 @@ impl MemFdSlot {
|
||||
self.image.is_some()
|
||||
}
|
||||
|
||||
#[allow(dead_code)] // ignore warnings as this is only used in some cfgs
|
||||
pub(crate) fn is_dirty(&self) -> bool {
|
||||
self.dirty
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user